import math
from openpyxl import load_workbook
from expert import Expert
from temp import Excel
from dataExpert import Data

def phase_shift_360(massive2):
    size = len(massive2) // 2
    y = []
    y_first_sector = []
    y_second_sector = []
    for i in range(size):
        y_first_sector.append(massive2[i])
    for i in range(size):
        y_second_sector.append(massive2[size+i])
    y.extend(y_second_sector)
    y.extend(y_first_sector)
    return y

def code_1(filename):
    expert = Expert()
    wb = load_workbook(filename + '/data_l4_res.xlsx')
    ws = wb["1"]
    ws2 = wb["2"]
    try:
        wb.create_sheet("5")
    except:
        print("I cant create sheet")
        pass

    # Диаметр цилиндра
    # DZ = 92
    DZ = ws.cell(row=2, column=7).value

    # радиус кривошипа
    # r = 43
    r = ws.cell(row=3, column=7).value

    # длина шатуна
    # l = 230
    l = ws.cell(row=4, column=7).value

    # степень сжатия
    # edw = 9.3
    edw = ws.cell(row=5, column=7).value

    # тип двигателя: 1 - бензин; 2 - дизель
    # kd = 1
    kd = ws.cell(row=6, column=7).value

    # Давление на впуске бензинового двигателя
    # pn1 = 1.02
    pn1 = ws.cell(row=7, column=7).value

    # Давление на впуске дизельного двигателя
    # pn2 = 1.033
    pn2 = ws.cell(row=8, column=7).value

    # Режим моментный -1 или мощностной-2
    # N1 = 1
    N1 = ws.cell(row=9, column=7).value

    # Максимальное давление  сгорания  Рzmax на режиме максимального крутящего момента
    # pz1 = 83
    pz1 = ws.cell(row=10, column=7).value

    # Максимальное давление сгорания Рzmax на режиме номинальной мощност
    # pz2 = 78
    pz2 = ws.cell(row=11, column=7).value

    if kd == 1: ws.cell(row=1, column=2).value = "PIND бензинового, кг/см2"
    if kd == 2: ws.cell(row=1, column=2).value = "PIND дизельного, кг/см2"

    # mDELT1 = DELT1
    # mDELT2 = DELT2
    # mDELT3 = DELT3

    Pi = 3.1415926535
    Rad = 0.0174532952
    Grad = 57.29577951
    FP = Pi / 4 * (DZ / 1000) ** 2
    VA = 7717.915 * edw / (7847.01 * (edw - 1) + 855.51)
    A00 = r / l
    A000 = edw / VA

    if N1 == 1: PN = pn1
    if N1 == 2: PN = pn2

    PA = (PN - 0.2) / 0.9407

    FSR = 0
    FSR1 = 0
    FSR2 = 0
    FSR3 = 0
    FMAX = 0
    FMAX1 = 0
    FMAX2 = 0
    FMAX3 = 0
    KP = 10

    if (kd == 1):
        if N1 == 1: PZM = pz1
        if N1 == 2: PZM = pz2

        for M1 in range(1, 231):
            PIND = PA
            ws.cell(row=M1 + 1, column=2).value = PIND
            ws2.cell(row=M1 + 1, column=10).value = PIND

        for M2 in range(231, 336):
            G1 = (1 - math.cos(M2 * Rad) + A00 / 2 - A00 * math.cos(M2 * Rad) ** 2 / 2) * (edw - 1) / 2 + 1
            PIND = PA * edw ** 1.38 * (1 / G1) ** 1.38
            ws.cell(row=M2 + 1, column=2).value = PIND
            ws2.cell(row=M2 + 1, column=10).value = PIND

        G2 = (edw - 1) * (1 + A00) / 4
        G3 = 0.05970301 * G2 + 1
        PIND335 = ws.cell(row=336, column=2).value
        G4 = ((0.1903795 * G2 + 1) / G3) ** 1.3 * PIND335
        # if (N1 == 1): PZM = PN * 7 * edw
        # if (N1 == 2): PZM = PN * 7 * edw - 5
        QZ = (PZM - G4) * G3 / 0.01153736 / A000
        A01 = (1 - (A00 * math.sin(335 * Rad)) ** 2)
        FIALF1 = 1 + (1 + 1 / A00 - math.cos(335 * Rad) - A01 / A00) * (edw - 1) / 2
        W01 = 0

        for m3 in range(336, 386):
            G4 = (math.acos(PIND335 / PZM)) / (Rad * -36)
            PIND = PZM * math.cos((Rad * (m3 - 336 - 35)) * G4)
            ws.cell(row=m3 + 1, column=2).value = PIND
            ws2.cell(row=m3 + 1, column=10).value = PIND

        #W01 = W02
        PIND385 = ws.cell(row=386, column=2).value
        G7 = PIND385 * ((1 - math.cos(385 * Rad) + A00 / 2 - A00 * math.cos(385 * Rad) ** 2 / 2) * (
                edw - 1) / 2 + 1) ** 1.25

        for M4 in range(386, 491):
            G5 = (1 - math.cos(M4 * Rad) + A00 / 2 - A00 * math.cos(M4 * Rad) ** 2 / 2) * (edw - 1) / 2 + 1
            PIND = G7 * (1 / G5) ** 1.25
            ws.cell(row=M4 + 1, column=2).value = PIND
            ws2.cell(row=M4 + 1, column=10).value = PIND

        PIND490 = ws.cell(row=491, column=2).value
        G6 = (1.15 - PIND490) / 110

        for M5 in range(491, 601):
            PIND = (M5 - 490) * G6 + PIND490
            ws.cell(row=M5 + 1, column=2).value = PIND
            ws2.cell(row=M5 + 1, column=10).value = PIND

        for M6 in range(601, 720):
            PIND = 1.15
            ws.cell(row=M6 + 1, column=2).value = PIND
            ws2.cell(row=M6 + 1, column=10).value = PIND
            ws.cell(row=721, column=2).value = 1
            ws2.cell(row=721, column=10).value = 1

    if (kd == 2):
        if (N1 == 1): PZM = pz1
        if (N1 == 2): PZM = pz2

        for N3 in range(1, 231):
            PIND = PA
            ws.cell(row=N3 + 1, column=2).value = PIND
            ws2.cell(row=N3 + 1, column=10).value = PIND

        for N4 in range(231, 360):
            G1 = (1 - math.cos(N4 * Rad) + A00 / 2 - A00 * math.cos(N4 * Rad) ** 2 / 2) * (edw - 1) / 2 + 1
            PIND = PA * edw ** 1.36 * (1 / G1) ** 1.36
            ws.cell(row=N4 + 1, column=2).value = PIND
            ws2.cell(row=N4 + 1, column=10).value = PIND

        G2 = (1 - math.cos(380 * Rad) + A00 / 2 - A00 * math.cos(380 * Rad) ** 2 / 2) * (edw - 1) / 2 + 1
        GKPI = PZM / (1 / G2) ** 1.3

        for N8 in range(360, 381):
            PIND = PZM
            ws.cell(row=N8 + 1, column=2).value = PIND
            ws2.cell(row=N8 + 1, column=10).value = PIND
            if (PIND <= PZM): PIND = 1

        for N5 in range(381, 491):
            G5 = (1 - math.cos(N5 * Rad) + A00 / 2 - A00 * math.cos(N5 * Rad) ** 2 / 2) * (edw - 1) / 2 + 1
            PIND = GKPI * (1 / G5) ** 1.3
            ws.cell(row=N5 + 1, column=2).value = PIND
            ws2.cell(row=N5 + 1, column=10).value = PIND

        for N6 in range(491, 601):
            PIND490 = ws.cell(row=491, column=2).value
            G6 = (1.15 - PIND490) / 110
            PIND = (N6 - 490) * G6 + PIND490
            ws.cell(row=N6 + 1, column=2).value = PIND
            ws2.cell(row=N6 + 1, column=10).value = PIND

        for N7 in range(601, 720):
            PIND = 1.15
            ws.cell(row=N7 + 1, column=2).value = PIND
            ws2.cell(row=N7 + 1, column=10).value = PIND

        ws.cell(row=721, column=2).value = 1
        ws2.cell(row=721, column=10).value = 1
    wb.save(filename + '/data_l4_res.xlsx')
    # excel = Excel(filename + '/data_l4_res.xlsx')
    # arr = excel.ParseFromFile()
    # arr = [i*10.197162 for i in arr]
    # excel.ParseToFile(arr, filename + '/data_l4_res.xlsx')
    if expert.change_phase == True:
        excel = Excel(filename + '/data_l4_res.xlsx')
        arr = excel.ParseFromFile()
        arr = phase_shift_360(arr)
        excel.ParseToFile(arr, filename + '/data_l4_res.xlsx')

def code_2(filename):
    wb = load_workbook(filename + '/data_l4_res.xlsx')
    ws = wb["2"]
    ws3 = wb["3"]
    ws4 = wb["4"]

    try:
        wb.create_sheet("5")
    except:
        print("I cant create sheet")
        pass

    Pi = 3.1415926535
    rad = 0.0174532952
    grad = 57.29577951308

    # Диаметр цилиндра
    dz = ws.cell(row=1, column=3).value
    # радиус кривошипа
    r = ws.cell(row=2, column=3).value
    # Длина шатуна
    l = ws.cell(row=3, column=3).value
    # Скорость вращения коленчатого вала
    nn = ws.cell(row=4, column=3).value
    # Масса поршня
    m = ws.cell(row=5, column=3).value
    # Масса верхней головки шатуна
    mw = ws.cell(row=6, column=3).value
    # Масса нижнец головки шатуна
    mp = ws.cell(row=7, column=3).value
    # Масса шатуна
    mcr = ws.cell(row=8, column=3).value
    # Момент инерции шатуна
    icr = ws.cell(row=9, column=3).value
    # Положение ЦТ шатуна
    pct = ws.cell(row=10, column=3).value
    # Способ расчета инерции шатуна
    Conrod = ws.cell(row=11, column=3).value
    # Способ расчета нуеравновешенных масс коленвала (переключение между расчетом из Fortran и новым расчетом)
    Conmass = ws.cell(row=12, column=3).value

    Fs = Pi * (dz / 20) ** 2
    A00 = r / l

    for k in range(1, 721):
        # Индикаторное давление в цилиндре
        Pz = ws.cell(row=k + 1, column=10).value
        # Вертикальное ускорение поршня
        az = (r / 1000) * (2 * Pi * nn / 60) ** 2 * (math.cos(k * rad) + A00 * math.cos(2 * k * rad))
        # Сила инерции поршня
        Fi = m * az
        # Газовая сила
        Fg = -Fs * (Pz - 1) * 9.80665
        # Угол отклонения шатуна
        delta = (math.asin(A00 * math.sin(k * rad))) * grad

        # Сила инерции верхней головки шатуна
        fiw = az * mw
        # Центробежная сила инерции нижней головки шатун
        fip = mp * r / 1000 * (2 * Pi * nn / 60) ** 2

        # Выбор способа расчета шатуна
        if Conrod == 1:

            # Расчет сил в КШМ по двухмассовой модели шатуна С ОШИБКОЙ ИЗ FORTRAN
            if Conmass == 1:
                A0 = (r / 1000) * (2 * Pi * nn / 60) ** 2

                PYZ = A0 * mp * (math.cos(k * rad) + A00 * math.cos(2 * k * rad)) - Fs * (Pz - 1) * 9.80665
                PXZ = -1 * PYZ * math.tan(delta * rad)

                FX = A0 * mw * math.sin((k + delta) * rad)
                FY = PYZ / math.cos(delta / grad) + A0 * mw * math.cos((k + delta) * rad)

            # Расчет сил в КШМ по двухмассовой модели шатуна ПРАВИЛЬНАЯ
            if Conmass == 2:
                FX = -(Fi + Fg + fiw) * math.sin(delta * rad) + fip * math.sin(k * rad)
                FY = Fi + Fg + fiw + fip * math.cos(k * rad)

                # строчка просто что бы вставить значения в дополнительные столбцы
                PXZ = FX
                PYZ = FY

                # Расчет сил в КШМ по одномассовой схеме
        if Conrod == 2:
            # Угловое ускорение шатуна
            adelta = -(2 * Pi * nn / 60) ** 2 * A00 * math.sin(k * rad)
            # Момент компенсирующий инерцию шатуна
            Madelta = icr * adelta
            # FX от момента инерци
            FXicr = Madelta / ((l / 1000) * math.cos(delta * rad))
            # FY от массы
            Fym = mcr * r / 1000 * (2 * Pi * nn / 60) ** 2 * (math.cos(k * rad) + (A00 * math.cos(2 * k * rad))) / pct
            # FX от массы
            FXm = mcr * r / 1000 * (2 * Pi * nn / 60) ** 2 * (math.sin(k * rad) * math.cos(delta * rad))
            # Силы КШМ
            FX = -(Fi + Fg) * math.sin(delta * rad) + FXicr + FXm
            FY = (Fi + Fg) + Fym

            # строчка просто что бы вставить значения в дополнительные столбцы
            PXZ = FX
            PYZ = FY

        FSUM = (FY ** 2 + FX ** 2) ** 0.5

        beta = math.atan(FX / FY) * grad
        if (k == 180) or (k == 540): beta = 0
        if (FY <= 0): beta = beta + 180
        if (FX <= 0) and (FY >= 0): beta = beta + 360
        if (FX > 0) and (FX <= 0.05) and (FY > 0): beta = 360

        # if (FX > 0) and (FY < 0): beta = beta + 180
        # if (FX < 0) and (FY < 0): beta = beta + 180
        # if (FX < 0) and (FY > 0): beta = beta + 360
        # if (FX > -0.05) and (FX < 0.05): beta = 360

        beta = beta + 180
        if (beta > 360): beta = beta - 360

        # Там в радианах все правильно, но здесь то почему градусы с радианами складываются
        gam = beta - k - (delta * rad)
        if (gam <= 0): gam = gam + 360
        if (gam <= 0): gam = gam + 360

        # Вывод данных
        ws.cell(row=1, column=11).value = "FX"
        ws.cell(row=k + 1, column=11).value = FX
        ws.cell(row=1, column=12).value = "FY"
        ws.cell(row=k + 1, column=12).value = FY
        ws.cell(row=1, column=13).value = "FSUM"
        ws.cell(row=k + 1, column=13).value = FSUM
        ws.cell(row=1, column=14).value = "beta"
        ws.cell(row=k + 1, column=14).value = beta
        ws.cell(row=1, column=15).value = "GAM"
        ws.cell(row=k + 1, column=15).value = gam
        ws.cell(row=1, column=17).value = "PXZ"
        ws.cell(row=k + 1, column=17).value = PXZ
        ws.cell(row=1, column=18).value = "PYZ"
        ws.cell(row=k + 1, column=18).value = PYZ

        # перенос в лист 3
        ws3.cell(row=k + 1, column=1).value = PXZ
        ws3.cell(row=k + 1, column=2).value = PYZ

        # перенос в лист 4
        ws4.cell(row=k + 1, column=10).value = FX
        ws4.cell(row=k + 1, column=11).value = FY
        ws4.cell(row=k + 1, column=12).value = FSUM
        ws4.cell(row=k + 1, column=13).value = beta
        ws4.cell(row=k + 1, column=14).value = gam

    wb.save(filename + '/data_l4_res.xlsx')



def code_3(filename):
    wb = load_workbook(filename + '/data_l4_res.xlsx')
    ws = wb["3"]
    ws4 = wb["4"]

    Pi = 3.1415926535
    RAD = 0.0174532952
    GRAD = 57.29577951308
    g = 9.80665
    nm = ws.cell(row=22, column=8).value
    R = ws.cell(row=23, column=8).value

    iwar = ws.cell(row=28, column=8).value
    conmass = ws.cell(row=41, column=8).value

    # Внешние силы
    Extforce1 = ws.cell(row=2, column=8).value
    Extforce2 = ws.cell(row=6, column=8).value
    Extforce3 = ws.cell(row=10, column=8).value
    Extforce4 = ws.cell(row=14, column=8).value
    Extforce5 = ws.cell(row=18, column=8).value
    Dirforce1 = ws.cell(row=3, column=8).value
    Dirforce2 = ws.cell(row=7, column=8).value
    Dirforce3 = ws.cell(row=11, column=8).value
    Dirforce4 = ws.cell(row=15, column=8).value
    Dirforce5 = ws.cell(row=19, column=8).value
    Armfoce1 = ws.cell(row=4, column=8).value
    Armfoce2 = ws.cell(row=8, column=8).value
    Armfoce3 = ws.cell(row=12, column=8).value
    Armfoce4 = ws.cell(row=16, column=8).value
    Armfoce5 = ws.cell(row=20, column=8).value

    # Размеры
    dl12 = ws.cell(row=30, column=8).value
    dl23 = ws.cell(row=31, column=8).value
    dl34 = ws.cell(row=32, column=8).value
    dl45 = ws.cell(row=33, column=8).value
    dz1 = ws.cell(row=35, column=8).value
    dz2 = ws.cell(row=36, column=8).value
    dz3 = ws.cell(row=37, column=8).value
    dz4 = ws.cell(row=38, column=8).value

    # Неуравновешенные силы conmass=1
    mk1 = ws.cell(row=44, column=8).value
    mk2 = ws.cell(row=46, column=8).value
    mk3 = ws.cell(row=48, column=8).value
    mk4 = ws.cell(row=50, column=8).value
    mk5 = ws.cell(row=52, column=8).value
    delt1 = ws.cell(row=45, column=8).value
    delt2 = ws.cell(row=47, column=8).value
    delt3 = ws.cell(row=49, column=8).value
    delt4 = ws.cell(row=51, column=8).value
    delt5 = ws.cell(row=53, column=8).value

    # Неуравновешенные силы conmass=2
    # между 1 и 2 коренной шейкой
    WR11 = ws.cell(row=56, column=8).value
    WR12 = ws.cell(row=60, column=8).value
    WR13 = ws.cell(row=64, column=8).value
    WR14 = ws.cell(row=68, column=8).value
    WR15 = ws.cell(row=72, column=8).value
    WR16 = ws.cell(row=76, column=8).value
    R11 = ws.cell(row=57, column=8).value
    R12 = ws.cell(row=62, column=8).value
    R13 = ws.cell(row=65, column=8).value
    R14 = ws.cell(row=69, column=8).value
    R15 = ws.cell(row=73, column=8).value
    R16 = ws.cell(row=77, column=8).value
    delt11 = ws.cell(row=58, column=8).value
    delt12 = ws.cell(row=62, column=8).value
    delt13 = ws.cell(row=66, column=8).value
    delt14 = ws.cell(row=70, column=8).value
    delt15 = ws.cell(row=74, column=8).value
    delt16 = ws.cell(row=78, column=8).value

    # между 2 и 3 коренной шейкой
    WR21 = ws.cell(row=80, column=8).value
    WR22 = ws.cell(row=84, column=8).value
    WR23 = ws.cell(row=88, column=8).value
    WR24 = ws.cell(row=92, column=8).value
    WR25 = ws.cell(row=96, column=8).value
    WR26 = ws.cell(row=100, column=8).value
    R21 = ws.cell(row=81, column=8).value
    R22 = ws.cell(row=85, column=8).value
    R23 = ws.cell(row=89, column=8).value
    R24 = ws.cell(row=93, column=8).value
    R25 = ws.cell(row=97, column=8).value
    R26 = ws.cell(row=101, column=8).value
    delt21 = ws.cell(row=82, column=8).value
    delt22 = ws.cell(row=86, column=8).value
    delt23 = ws.cell(row=90, column=8).value
    delt24 = ws.cell(row=94, column=8).value
    delt25 = ws.cell(row=98, column=8).value
    delt26 = ws.cell(row=102, column=8).value

    # между 3 и 4 коренной шейкой
    WR31 = ws.cell(row=104, column=8).value
    WR32 = ws.cell(row=108, column=8).value
    WR33 = ws.cell(row=112, column=8).value
    WR34 = ws.cell(row=116, column=8).value
    WR35 = ws.cell(row=120, column=8).value
    WR36 = ws.cell(row=124, column=8).value
    R31 = ws.cell(row=105, column=8).value
    R32 = ws.cell(row=109, column=8).value
    R33 = ws.cell(row=113, column=8).value
    R34 = ws.cell(row=117, column=8).value
    R35 = ws.cell(row=121, column=8).value
    R36 = ws.cell(row=125, column=8).value
    delt31 = ws.cell(row=106, column=8).value
    delt32 = ws.cell(row=110, column=8).value
    delt33 = ws.cell(row=114, column=8).value
    delt34 = ws.cell(row=118, column=8).value
    delt35 = ws.cell(row=122, column=8).value
    delt36 = ws.cell(row=126, column=8).value

    # между 4 и 5 коренной шейкой
    WR41 = ws.cell(row=127, column=8).value
    WR42 = ws.cell(row=131, column=8).value
    WR43 = ws.cell(row=135, column=8).value
    WR44 = ws.cell(row=139, column=8).value
    WR45 = ws.cell(row=143, column=8).value
    WR46 = ws.cell(row=147, column=8).value
    R41 = ws.cell(row=128, column=8).value
    R42 = ws.cell(row=132, column=8).value
    R43 = ws.cell(row=136, column=8).value
    R44 = ws.cell(row=140, column=8).value
    R45 = ws.cell(row=144, column=8).value
    R46 = ws.cell(row=148, column=8).value
    delt41 = ws.cell(row=129, column=8).value
    delt42 = ws.cell(row=133, column=8).value
    delt43 = ws.cell(row=137, column=8).value
    delt44 = ws.cell(row=141, column=8).value
    delt45 = ws.cell(row=145, column=8).value
    delt46 = ws.cell(row=149, column=8).value

    # Силы в коренных подшипниках от внешних сил
    FXKarmforce1 = ((Extforce1 * math.sin(Dirforce1 / GRAD)) * (Armfoce1 + dl12)) / dl12 + (
                (Extforce2 * math.sin(Dirforce2 / GRAD)) * (Armfoce2 + dl12)) / dl12 + (
                               (Extforce3 * math.sin(Dirforce3 / GRAD)) * (Armfoce3 + dl12)) / dl12 + (
                               (Extforce4 * math.sin(Dirforce4 / GRAD)) * (Armfoce4 + dl12)) / dl12 + (
                               (Extforce5 * math.sin(Dirforce5 / GRAD)) * (Armfoce5 + dl12)) / dl12
    FYKarmforce1 = (((Extforce1 * math.cos(Dirforce1 / GRAD)) * (Armfoce1 + dl12)) / dl12) + (
                ((Extforce2 * math.cos(Dirforce2 / GRAD)) * (Armfoce2 + dl12)) / dl12) + (
                               ((Extforce3 * math.cos(Dirforce3 / GRAD)) * (Armfoce3 + dl12)) / dl12) + (
                               ((Extforce4 * math.cos(Dirforce4 / GRAD)) * (Armfoce4 + dl12)) / dl12) + (
                               ((Extforce5 * math.cos(Dirforce5 / GRAD)) * (Armfoce5 + dl12)) / dl12)

    FXKarmforce2 = -(Extforce1 * math.sin(Dirforce1 / GRAD) * Armfoce1 / dl12) - (
                Extforce2 * math.sin(Dirforce2 / GRAD) * Armfoce2 / dl12) - (
                               Extforce3 * math.sin(Dirforce3 / GRAD) * Armfoce3 / dl12) - (
                               Extforce4 * math.sin(Dirforce4 / GRAD) * Armfoce4 / dl12) - (
                               Extforce5 * math.sin(Dirforce5 / GRAD) * Armfoce5 / dl12)
    FYKarmforce2 = -(Extforce1 * math.cos(Dirforce1 / GRAD) * Armfoce1 / dl12) - (
                Extforce2 * math.cos(Dirforce2 / GRAD) * Armfoce2 / dl12) - (
                               Extforce3 * math.cos(Dirforce3 / GRAD) * Armfoce3 / dl12) - (
                               Extforce4 * math.cos(Dirforce4 / GRAD) * Armfoce4 / dl12) - (
                               Extforce5 * math.cos(Dirforce5 / GRAD) * Armfoce5 / dl12)

    A0 = R / 1000 * (2 * Pi * nm / 60) ** 2

    for k in range(1, 721):
        k1 = k
        if iwar == 1:
            k2 = k - 180
            k3 = k - 540
            k4 = k - 360

        if iwar == 2:
            k2 = k1 - 540
            k3 = k1 - 180
            k4 = k1 - 360

        if (k2 <= 0): k2 = k2 + 720
        if (k3 <= 0): k3 = k3 + 720
        if (k4 <= 0): k4 = k4 + 720

        # Силы в коренных подшипниках + инерционныt силs conmass=1
        if conmass == 1:
            FX1 = ws.cell(row=k1 + 1, column=1).value
            FY1 = ws.cell(row=k1 + 1, column=2).value
            # Силы действующие на 1 коренной подшипник conmass=1
            FXK1 = FX1 * ((dl12 - dz1) / dl12) + A0 * mk1 * math.sin((k + delt1) / GRAD) + FXKarmforce1
            FYK1 = FY1 * ((dl12 - dz1) / dl12) + A0 * mk1 * math.cos((k + delt1) / GRAD) + FYKarmforce1

            # Силы действующие на 2 коренной подшипник conmass=1
            FX2 = ws.cell(row=k2 + 1, column=1).value
            FY2 = ws.cell(row=k2 + 1, column=2).value

            FXK21 = FX1 * (dz1 / dl12)
            FYK21 = FY1 * (dz1 / dl12)
            FXK22 = FX2 * ((dl23 - dz2) / dl23)
            FYK22 = FY2 * ((dl23 - dz2) / dl23)

            FXK2 = FXK21 + FXK22 + A0 * mk2 * math.sin((k + delt2) / GRAD) + FXKarmforce2
            FYK2 = FYK21 + FYK22 + A0 * mk2 * math.cos((k + delt2) / GRAD) + FYKarmforce2
            # Силы действующие на 3 коренной подшипник conmass=1
            FX3 = ws.cell(row=k3 + 1, column=1).value
            FY3 = ws.cell(row=k3 + 1, column=2).value

            FXK31 = FX2 * (dz2 / dl23)
            FYK31 = FY2 * (dz2 / dl23)
            FXK32 = FX3 * ((dl34 - dz3) / dl34)
            FYK32 = FY3 * ((dl34 - dz3) / dl34)

            FXK3 = FXK31 + FXK32 + A0 * mk3 * math.sin((k + delt3) / GRAD)
            FYK3 = FYK31 + FYK32 + A0 * mk3 * math.cos((k + delt3) / GRAD)

            # Силы действующие на 4 коренной подшипник conmass=1
            FX4 = ws.cell(row=k4 + 1, column=1).value
            FY4 = ws.cell(row=k4 + 1, column=2).value

            FXK41 = FX3 * (dz3 / dl34)
            FYK41 = FY3 * (dz3 / dl34)
            FXK42 = FX4 * ((dl45 - dz4) / dl45)
            FYK42 = FY4 * ((dl45 - dz4) / dl45)

            FXK4 = FXK41 + FXK42 + A0 * mk4 * math.sin((k + delt4) / GRAD)
            FYK4 = FYK41 + FYK42 + A0 * mk4 * math.cos((k + delt4) / GRAD)

            # Силы действующие на 5 коренной подшипник conmass=1
            FXK5 = FX4 * (dz4 / dl45) + A0 * mk5 * math.sin((k + delt5) / GRAD)
            FYK5 = FY4 * (dz4 / dl45) + A0 * mk5 * math.cos((k + delt5) / GRAD)

        # Силы в коренных подшипниках + инерционныt силs conmass=2
        if conmass == 2:
            A0 = (2 * Pi * nm / 60) ** 2
            FX1 = ws.cell(row=k1 + 1, column=1).value
            FY1 = ws.cell(row=k1 + 1, column=2).value
            # Силы действующие на 1 коренной подшипник conmass=2
            FXU12 = A0 * WR11 / g * math.sin((k + delt11) / GRAD) * ((dl12 - R11) / dl12) + A0 * WR12 / g * math.sin(
                (k + delt12) / GRAD) * ((dl12 - R12) / dl12) + A0 * WR13 / g * math.sin((k + delt13) / GRAD) * (
                                (dl12 - R13) / dl12) + A0 * WR14 / g * math.sin((k + delt14) / GRAD) * (
                                (dl12 - R14) / dl12) + A0 * WR15 / g * math.sin((k + delt15) / GRAD) * (
                                (dl12 - R15) / dl12) + A0 * WR16 / g * math.sin((k + delt16) / GRAD) * (
                                (dl12 - R16) / dl12)
            FYU12 = A0 * WR11 / g * math.cos((k + delt11) / GRAD) * ((dl12 - R11) / dl12) + A0 * WR12 / g * math.cos(
                (k + delt12) / GRAD) * ((dl12 - R12) / dl12) + A0 * WR13 / g * math.cos((k + delt13) / GRAD) * (
                                (dl12 - R13) / dl12) + A0 * WR14 / g * math.cos((k + delt14) / GRAD) * (
                                (dl12 - R14) / dl12) + A0 * WR15 / g * math.cos((k + delt15) / GRAD) * (
                                (dl12 - R15) / dl12) + A0 * WR16 / g * math.cos((k + delt16) / GRAD) * (
                                (dl12 - R16) / dl12)

            FXK1 = FX1 * ((dl12 - dz1) / dl12) + FXKarmforce1 + FXU12
            FYK1 = FY1 * ((dl12 - dz1) / dl12) + FYKarmforce1 + FYU12
            # Силы действующие на 2 коренной подшипник conmass=2
            FX2 = ws.cell(row=k2 + 1, column=1).value
            FY2 = ws.cell(row=k2 + 1, column=2).value

            FXU21 = A0 * WR11 / g * math.sin((k + delt11) / GRAD) * (R11 / dl12) + A0 * WR12 / g * math.sin(
                (k + delt12) / GRAD) * (R12 / dl12) + A0 * WR13 / g * math.sin((k + delt13) / GRAD) * (
                                R13 / dl12) + A0 * WR14 / g * math.sin((k + delt14) / GRAD) * (
                                R14 / dl12) + A0 * WR15 / g * math.sin((k + delt15) / GRAD) * (
                                R15 / dl12) + A0 * WR16 / g * math.sin((k + delt16) / GRAD) * (R16 / dl12)
            FYU21 = A0 * WR11 / g * math.cos((k + delt11) / GRAD) * (R11 / dl12) + A0 * WR12 / g * math.cos(
                (k + delt12) / GRAD) * (R12 / dl12) + A0 * WR13 / g * math.cos((k + delt13) / GRAD) * (
                                R13 / dl12) + A0 * WR14 / g * math.cos((k + delt14) / GRAD) * (
                                (dl12 - R14) / dl12) + A0 * WR15 / g * math.cos((k + delt15) / GRAD) * (
                                R15 / dl12) + A0 * WR16 / g * math.cos((k + delt16) / GRAD) * (R16 / dl12)
            FXU22 = A0 * WR21 / g * math.sin((k + delt21) / GRAD) * ((dl23 - R21) / dl23) + A0 * WR22 / g * math.sin(
                (k + delt22) / GRAD) * ((dl23 - R22) / dl23) + A0 * WR23 / g * math.sin((k + delt23) / GRAD) * (
                                (dl23 - R23) / dl23) + A0 * WR24 / g * math.sin((k + delt24) / GRAD) * (
                                (dl23 - R24) / dl23) + A0 * WR25 / g * math.sin((k + delt25) / GRAD) * (
                                (dl23 - R25) / dl23) + A0 * WR26 / g * math.sin((k + delt26) / GRAD) * (
                                (dl23 - R26) / dl23)
            FYU22 = A0 * WR21 / g * math.cos((k + delt21) / GRAD) * ((dl23 - R21) / dl23) + A0 * WR22 / g * math.cos(
                (k + delt22) / GRAD) * ((dl23 - R22) / dl23) + A0 * WR23 / g * math.cos((k + delt23) / GRAD) * (
                                (dl23 - R23) / dl23) + A0 * WR24 / g * math.cos((k + delt24) / GRAD) * (
                                (dl23 - R24) / dl23) + A0 * WR25 / g * math.cos((k + delt25) / GRAD) * (
                                (dl23 - R25) / dl23) + A0 * WR26 / g * math.cos((k + delt26) / GRAD) * (
                                (dl23 - R26) / dl23)

            FXK21 = FX1 * (dz1 / dl12)
            FYK21 = FY1 * (dz1 / dl12)
            FXK22 = FX2 * ((dl23 - dz2) / dl23)
            FYK22 = FY2 * ((dl23 - dz2) / dl23)

            FXK2 = FXK21 + FXK22 + FXKarmforce2 + FXU21 + FXU22
            FYK2 = FYK21 + FYK22 + FYKarmforce2 + FYU21 + FYU22

            # Силы действующие на 3 коренной подшипник conmass=2
            FX3 = ws.cell(row=k3 + 1, column=1).value
            FY3 = ws.cell(row=k3 + 1, column=2).value

            FXU31 = A0 * WR21 / g * math.sin((k + delt21) / GRAD) * (R21 / dl23) + A0 * WR22 / g * math.sin(
                (k + delt22) / GRAD) * (R22 / dl23) + A0 * WR23 / g * math.sin((k + delt23) / GRAD) * (
                                R23 / dl23) + A0 * WR24 / g * math.sin((k + delt24) / GRAD) * (
                                R24 / dl23) + A0 * WR25 / g * math.sin((k + delt25) / GRAD) * (
                                R25 / dl23) + A0 * WR26 / g * math.sin((k + delt26) / GRAD) * (R26 / dl23)
            FYU31 = A0 * WR21 / g * math.cos((k + delt21) / GRAD) * (R21 / dl23) + A0 * WR22 / g * math.cos(
                (k + delt22) / GRAD) * (R22 / dl23) + A0 * WR23 / g * math.cos((k + delt23) / GRAD) * (
                                R23 / dl23) + A0 * WR24 / g * math.cos((k + delt24) / GRAD) * (
                                R24 / dl23) + A0 * WR25 / g * math.cos((k + delt25) / GRAD) * (
                                R25 / dl23) + A0 * WR26 / g * math.cos((k + delt26) / GRAD) * (R26 / dl23)
            FXU32 = A0 * WR31 / g * math.sin((k + delt31) / GRAD) * ((dl34 - R31) / dl34) + A0 * WR32 / g * math.sin(
                (k + delt32) / GRAD) * ((dl34 - R32) / dl34) + A0 * WR33 / g * math.sin((k + delt33) / GRAD) * (
                                (dl34 - R33) / dl34) + A0 * WR34 / g * math.sin((k + delt34) / GRAD) * (
                                (dl34 - R34) / dl34) + A0 * WR35 / g * math.sin((k + delt35) / GRAD) * (
                                (dl34 - R35) / dl34) + A0 * WR36 / g * math.sin((k + delt36) / GRAD) * (
                                (dl34 - R36) / dl34)
            FYU32 = A0 * WR31 / g * math.cos((k + delt31) / GRAD) * ((dl34 - R31) / dl34) + A0 * WR32 / g * math.cos(
                (k + delt32) / GRAD) * ((dl34 - R32) / dl34) + A0 * WR33 / g * math.cos((k + delt33) / GRAD) * (
                                (dl34 - R33) / dl34) + A0 * WR34 / g * math.cos((k + delt34) / GRAD) * (
                                (dl34 - R34) / dl34) + A0 * WR35 / g * math.cos((k + delt35) / GRAD) * (
                                (dl34 - R35) / dl34) + A0 * WR36 / g * math.cos((k + delt36) / GRAD) * (
                                (dl34 - R36) / dl34)

            FXK31 = FX2 * (dz2 / dl23)
            FYK31 = FY2 * (dz2 / dl23)
            FXK32 = FX3 * ((dl34 - dz3) / dl34)
            FYK32 = FY3 * ((dl34 - dz3) / dl34)

            FXK3 = FXK31 + FXK32 + FXU31 + FXU32
            FYK3 = FYK31 + FYK32 + FYU31 + FYU32

            # Силы действующие на 4 коренной подшипник conmass=2
            FX4 = ws.cell(row=k4 + 1, column=1).value
            FY4 = ws.cell(row=k4 + 1, column=2).value

            FXU41 = A0 * WR31 / g * math.sin((k + delt31) / GRAD) * (R31 / dl34) + A0 * WR32 / g * math.sin(
                (k + delt32) / GRAD) * (R32 / dl34) + A0 * WR33 / g * math.sin((k + delt33) / GRAD) * (
                                R33 / dl34) + A0 * WR34 / g * math.sin((k + delt34) / GRAD) * (
                                R34 / dl34) + A0 * WR35 / g * math.sin((k + delt35) / GRAD) * (
                                R35 / dl34) + A0 * WR36 / g * math.sin((k + delt36) / GRAD) * (R36 / dl34)
            FYU41 = A0 * WR31 / g * math.cos((k + delt31) / GRAD) * (R31 / dl34) + A0 * WR32 / g * math.cos(
                (k + delt32) / GRAD) * (R32 / dl34) + A0 * WR33 / g * math.cos((k + delt33) / GRAD) * (
                                R33 / dl34) + A0 * WR34 / g * math.cos((k + delt34) / GRAD) * (
                                R34 / dl34) + A0 * WR35 / g * math.cos((k + delt35) / GRAD) * (
                                R35 / dl34) + A0 * WR36 / g * math.cos((k + delt36) / GRAD) * (R36 / dl34)
            FXU42 = A0 * WR41 / g * math.sin((k + delt41) / GRAD) * ((dl45 - R41) / dl45) + A0 * WR42 / g * math.sin(
                (k + delt42) / GRAD) * ((dl45 - R42) / dl45) + A0 * WR43 / g * math.sin((k + delt43) / GRAD) * (
                                (dl45 - R43) / dl45) + A0 * WR44 / g * math.sin((k + delt44) / GRAD) * (
                                (dl45 - R44) / dl45) + A0 * WR45 / g * math.sin((k + delt45) / GRAD) * (
                                (dl45 - R45) / dl45) + A0 * WR46 / g * math.sin((k + delt46) / GRAD) * (
                                (dl45 - R46) / dl45)
            FYU42 = A0 * WR41 / g * math.cos((k + delt41) / GRAD) * ((dl45 - R41) / dl45) + A0 * WR42 / g * math.cos(
                (k + delt42) / GRAD) * ((dl45 - R42) / dl45) + A0 * WR43 / g * math.cos((k + delt43) / GRAD) * (
                                (dl45 - R43) / dl45) + A0 * WR44 / g * math.cos((k + delt44) / GRAD) * (
                                (dl45 - R44) / dl45) + A0 * WR45 / g * math.cos((k + delt45) / GRAD) * (
                                (dl45 - R45) / dl45) + A0 * WR46 / g * math.cos((k + delt46) / GRAD) * (
                                (dl45 - R46) / dl45)

            FXK41 = FX3 * (dz3 / dl34)
            FYK41 = FY3 * (dz3 / dl34)
            FXK42 = FX4 * ((dl45 - dz4) / dl45)
            FYK42 = FY4 * ((dl45 - dz4) / dl45)

            FXK4 = FXK41 + FXK42 + FXU41 + FXU42
            FYK4 = FYK41 + FYK42 + FYU41 + FYU42

            # Силы действующие на 5 коренной подшипник conmass=1
            FXU51 = A0 * WR41 / g * math.sin((k + delt41) / GRAD) * (R41 / dl45) + A0 * WR42 / g * math.sin(
                (k + delt42) / GRAD) * (R42 / dl45) + A0 * WR43 / g * math.sin((k + delt43) / GRAD) * (
                                R43 / dl45) + A0 * WR44 / g * math.sin((k + delt44) / GRAD) * (
                                R44 / dl45) + A0 * WR45 / g * math.sin((k + delt45) / GRAD) * (
                                R45 / dl45) + A0 * WR46 / g * math.sin((k + delt46) / GRAD) * (R46 / dl45)
            FYU51 = A0 * WR41 / g * math.cos((k + delt41) / GRAD) * (R41 / dl45) + A0 * WR42 / g * math.cos(
                (k + delt42) / GRAD) * (R42 / dl45) + A0 * WR43 / g * math.cos((k + delt43) / GRAD) * (
                                R43 / dl45) + A0 * WR44 / g * math.cos((k + delt44) / GRAD) * (
                                R44 / dl45) + A0 * WR45 / g * math.cos((k + delt45) / GRAD) * (
                                R45 / dl45) + A0 * WR46 / g * math.cos((k + delt46) / GRAD) * (R46 / dl45)

            FXK5 = FX4 * (dz4 / dl45) + FXU51
            FYK5 = FY4 * (dz4 / dl45) + FYU51

        # Расчет суммарных сил и углов
        FSUM1 = (FXK1 ** 2 + FYK1 ** 2) ** 0.5
        FSUM2 = (FXK2 ** 2 + FYK2 ** 2) ** 0.5
        FSUM3 = (FXK3 ** 2 + FYK3 ** 2) ** 0.5
        FSUM4 = (FXK4 ** 2 + FYK4 ** 2) ** 0.5
        FSUM5 = (FXK5 ** 2 + FYK5 ** 2) ** 0.5

        BETA1 = math.atan(FXK1 / FYK1) * GRAD
        if (k == 180 or k == 540): BETA1 = 0
        if (FYK1 <= 0): BETA1 = BETA1 + 180
        if (FXK1 <= 0 and FYK1 >= 0): BETA1 = BETA1 + 360
        if (FXK1 > 0 and FXK1 < 2.8 and FYK1 > 0): BETA1 = 360
        BETA2 = math.atan(FXK2 / FYK2) * GRAD
        if (k == 180 or k == 540): BETA2 = 0
        if (FYK2 <= 0): BETA2 = BETA2 + 180
        if (FXK2 <= 0 and FYK2 >= 0): BETA2 = BETA2 + 360
        if (FXK2 > 0 and FXK2 < 2.8 and FYK2 > 0): BETA2 = 360
        BETA3 = math.atan(FXK3 / FYK3) * GRAD
        if (k == 180 or k == 540): BETA3 = 0
        if (FYK3 <= 0): BETA3 = BETA3 + 180
        if (FXK3 <= 0 and FYK3 >= 0): BETA3 = BETA3 + 360
        if (FXK3 > 0 and FXK3 < 2.8 and FYK3 > 0): BETA3 = 360
        BETA4 = math.atan(FXK4 / FYK4) * GRAD
        if (k == 180 or k == 540): BETA4 = 0
        if (FYK4 <= 0): BETA4 = BETA4 + 180
        if (FXK4 <= 0 and FYK4 >= 0): BETA4 = BETA4 + 360
        if (FXK4 > 0 and FXK4 < 2.8 and FYK4 > 0): BETA4 = 360
        BETA5 = math.atan(FXK5 / FYK5) * GRAD
        if (k == 180 or k == 540): BETA5 = 0
        if (FYK5 <= 0): BETA5 = BETA5 + 180
        if (FXK5 <= 0 and FYK5 >= 0): BETA5 = BETA5 + 360
        if (FXK5 > 0 and FXK5 < 2.8 and FYK5 > 0): BETA5 = 360

        GAM1 = BETA1 - k
        if (GAM1 <= 0): GAM1 = GAM1 + 360
        if (GAM1 <= 0): GAM1 = GAM1 + 360
        GAM2 = BETA2 - k
        if (GAM2 <= 0): GAM2 = GAM2 + 360
        if (GAM2 <= 0): GAM2 = GAM2 + 360
        GAM3 = BETA3 - k
        if (GAM3 <= 0): GAM3 = GAM3 + 360
        if (GAM3 <= 0): GAM3 = GAM3 + 360
        GAM4 = BETA4 - k
        if (GAM4 <= 0): GAM4 = GAM4 + 360
        if (GAM4 <= 0): GAM4 = GAM4 + 360
        GAM5 = BETA5 - k
        if (GAM5 <= 0): GAM5 = GAM5 + 360
        if (GAM5 <= 0): GAM5 = GAM5 + 360

        # Вывод результатов
        ws.cell(row=1, column=11).value = "k"
        ws.cell(row=k + 1, column=11).value = k
        # Коренная шейка 1
        ws.cell(row=1, column=12).value = "FXK1"
        ws.cell(row=k + 1, column=12).value = FXK1
        ws.cell(row=1, column=13).value = "FYK1"
        ws.cell(row=k + 1, column=13).value = FYK1
        ws.cell(row=1, column=14).value = "FSUM1"
        ws.cell(row=k + 1, column=14).value = FSUM1
        ws.cell(row=1, column=15).value = "BETA1"
        ws.cell(row=k + 1, column=15).value = BETA1
        ws.cell(row=1, column=16).value = "GAM1"
        ws.cell(row=k + 1, column=16).value = GAM1
        # Коренная шейка 2
        ws.cell(row=1, column=17).value = "FXK2"
        ws.cell(row=k + 1, column=17).value = FXK2
        ws.cell(row=1, column=18).value = "FYK2"
        ws.cell(row=k + 1, column=18).value = FYK2
        ws.cell(row=1, column=19).value = "FSUM2"
        ws.cell(row=k + 1, column=19).value = FSUM2
        ws.cell(row=1, column=20).value = "BETA2"
        ws.cell(row=k + 1, column=20).value = BETA2
        ws.cell(row=1, column=21).value = "GAM2"
        ws.cell(row=k + 1, column=21).value = GAM2
        # Коренная шейка 3
        ws.cell(row=1, column=22).value = "FXK3"
        ws.cell(row=k + 1, column=22).value = FXK3
        ws.cell(row=1, column=23).value = "FYK3"
        ws.cell(row=k + 1, column=23).value = FYK3
        ws.cell(row=1, column=24).value = "FSUM3"
        ws.cell(row=k + 1, column=24).value = FSUM3
        ws.cell(row=1, column=25).value = "BETA3"
        ws.cell(row=k + 1, column=25).value = BETA3
        ws.cell(row=1, column=26).value = "GAM3"
        ws.cell(row=k + 1, column=26).value = GAM3
        # Коренная шейка 4
        ws.cell(row=1, column=27).value = "FXK4"
        ws.cell(row=k + 1, column=27).value = FXK4
        ws.cell(row=1, column=28).value = "FYK4"
        ws.cell(row=k + 1, column=28).value = FYK4
        ws.cell(row=1, column=29).value = "FSUM4"
        ws.cell(row=k + 1, column=29).value = FSUM4
        ws.cell(row=1, column=30).value = "BETA4"
        ws.cell(row=k + 1, column=30).value = BETA4
        ws.cell(row=1, column=31).value = "GAM4"
        ws.cell(row=k + 1, column=31).value = GAM4
        # Коренная шейка 5
        ws.cell(row=1, column=32).value = "FXK5"
        ws.cell(row=k + 1, column=32).value = FXK5
        ws.cell(row=1, column=33).value = "FYK5"
        ws.cell(row=k + 1, column=33).value = FYK5
        ws.cell(row=1, column=34).value = "FSUM5"
        ws.cell(row=k + 1, column=34).value = FSUM5
        ws.cell(row=1, column=35).value = "BETA5"
        ws.cell(row=k + 1, column=35).value = BETA5
        ws.cell(row=1, column=36).value = "GAM5"
        ws.cell(row=k + 1, column=36).value = GAM5



        # перенос в 4
        ws4.cell(row=k + 1, column=15).value = FXK1
        ws4.cell(row=k + 1, column=16).value = FYK1
        ws4.cell(row=k + 1, column=17).value = FSUM1
        ws4.cell(row=k + 1, column=18).value = BETA1
        ws4.cell(row=k + 1, column=19).value = GAM1

        ws4.cell(row=k + 1, column=20).value = FXK2
        ws4.cell(row=k + 1, column=21).value = FYK2
        ws4.cell(row=k + 1, column=22).value = FSUM2
        ws4.cell(row=k + 1, column=23).value = BETA2
        ws4.cell(row=k + 1, column=24).value = GAM2

        ws4.cell(row=k + 1, column=25).value = FXK3
        ws4.cell(row=k + 1, column=26).value = FYK3
        ws4.cell(row=k + 1, column=27).value = FSUM3
        ws4.cell(row=k + 1, column=28).value = BETA3
        ws4.cell(row=k + 1, column=29).value = GAM3

        ws4.cell(row=k + 1, column=30).value = FXK4
        ws4.cell(row=k + 1, column=31).value = FYK4
        ws4.cell(row=k + 1, column=32).value = FSUM4
        ws4.cell(row=k + 1, column=33).value = BETA4
        ws4.cell(row=k + 1, column=34).value = GAM4

        ws4.cell(row=k + 1, column=35).value = FXK5
        ws4.cell(row=k + 1, column=36).value = FYK5
        ws4.cell(row=k + 1, column=37).value = FSUM5
        ws4.cell(row=k + 1, column=38).value = BETA5
        ws4.cell(row=k + 1, column=39).value = GAM5

    wb.save(filename + '/data_l4_res.xlsx')


def code_4(filename, fl):
    wb = load_workbook(filename + '/data_l4_res.xlsx')
    ws = wb["4"]
    data = Data()
    nm = ws.cell(row=1, column=4).value
    dsch = ws.cell(row=2, column=4).value
    dkor = ws.cell(row=3, column=4).value
    bsch = ws.cell(row=4, column=4).value
    BKOR1 = ws.cell(row=5, column=4).value
    BKOR2 = ws.cell(row=6, column=4).value
    BKOR3 = ws.cell(row=7, column=4).value
    BKOR4 = ws.cell(row=8, column=4).value
    BKOR5 = ws.cell(row=9, column=4).value
    # BKOR6 = ws.cell(row=10, column=4).value
    # BKOR7 = ws.cell(row=11, column=4).value

    BKAN1 = ws.cell(row=12, column=4).value
    BKAN2 = ws.cell(row=13, column=4).value
    BKAN3 = ws.cell(row=14, column=4).value
    BKAN4 = ws.cell(row=15, column=4).value
    BKAN5 = ws.cell(row=16, column=4).value
    # BKAN6 = ws.cell(row=17, column=4).value
    # BKAN7 = ws.cell(row=18, column=4).value

    TVX = ws.cell(row=33, column=4).value
    d0 = ws.cell(row=34, column=4).value
    PM = ws.cell(row=40, column=4).value
    SSCH = ws.cell(row=41, column=4).value
    SKOR = ws.cell(row=42, column=4).value
    lw = ws.cell(row=43, column=4).value
    SSCHT = ws.cell(row=44, column=4).value
    SKORT = ws.cell(row=45, column=4).value
    NW = ws.cell(row=46, column=4).value
    ms = ws.cell(row=47, column=4).value
    fks = ws.cell(row=48, column=4).value
    fkk = ws.cell(row=49, column=4).value
    r = ws.cell(row=50, column=4).value
    l = ws.cell(row=51, column=4).value

    Pi = 3.1415926535
    rad = 0.01745329252
    GRAD = 57.29577951

    ww = Pi * nm / 30
    A00 = r / l

    for mm in range(1, 721):
        FSUM = ws.cell(row=mm + 1, column=12).value
        FSUM1 = ws.cell(row=mm + 1, column=17).value
        FSUM2 = ws.cell(row=mm + 1, column=22).value
        FSUM3 = ws.cell(row=mm + 1, column=27).value
        FSUM4 = ws.cell(row=mm + 1, column=32).value
        FSUM5 = ws.cell(row=mm + 1, column=37).value
        FSUM6 = ws.cell(row=mm + 1, column=42).value
        FSUM7 = ws.cell(row=mm + 1, column=47).value

        BETA1 = ws.cell(row=mm + 1, column=18).value
        BETA2 = ws.cell(row=mm + 1, column=23).value
        BETA3 = ws.cell(row=mm + 1, column=28).value
        BETA4 = ws.cell(row=mm + 1, column=33).value
        BETA5 = ws.cell(row=mm + 1, column=38).value
        BETA6 = ws.cell(row=mm + 1, column=43).value
        BETA7 = ws.cell(row=mm + 1, column=48).value

        if NW == 0:
            PSUM = FSUM / dsch / bsch
            PSUM1 = FSUM1 / BKOR1 / dkor
            PSUM2 = FSUM2 / BKOR2 / dkor
            PSUM3 = FSUM3 / BKOR3 / dkor
            PSUM4 = FSUM4 / BKOR4 / dkor
            PSUM5 = FSUM5 / BKOR5 / dkor

        if NW == 1:
            PSUM = FSUM / dsch / bsch
            PSUM1 = FSUM1 / (BKOR1 - BKAN1) / dkor
            PSUM2 = FSUM2 / (BKOR2 - BKAN2) / dkor
            PSUM3 = FSUM3 / (BKOR3 - BKAN3) / dkor
            PSUM4 = FSUM4 / (BKOR4 - BKAN4) / dkor
            PSUM5 = FSUM5 / (BKOR5 - BKAN5) / dkor

        if NW == 2 or NW == 3:
            PSUM = FSUM / dsch / bsch
            if BETA1 >= 90 and BETA1 <= 270: PSUM1 = FSUM1 / BKOR1 / dkor
            if BETA1 < 90 or BETA1 > 270: PSUM1 = FSUM1 / (BKOR1 - BKAN1) / dkor
            if BETA2 >= 90 and BETA2 <= 270: PSUM2 = FSUM2 / BKOR2 / dkor
            if BETA2 < 90 or BETA2 > 270: PSUM2 = FSUM2 / (BKOR2 - BKAN2) / dkor
            if BETA3 >= 90 and BETA3 <= 270: PSUM3 = FSUM3 / BKOR3 / dkor
            if BETA3 < 90 or BETA3 > 270: PSUM3 = FSUM3 / (BKOR3 - BKAN3) / dkor
            if BETA4 >= 90 and BETA4 <= 270: PSUM4 = FSUM4 / BKOR4 / dkor
            if BETA4 < 90 or BETA4 > 270: PSUM4 = FSUM4 / (BKOR4 - BKAN4) / dkor
            if BETA5 >= 90 and BETA5 <= 270: PSUM5 = FSUM5 / BKOR5 / dkor
            if BETA5 < 90 or BETA5 > 270: PSUM5 = FSUM5 / (BKOR5 - BKAN5) / dkor
            #if BETA6 >= 90 and BETA6 <= 270: PSUM6 = FSUM6 / BKOR6 / dkor
            #if BETA6 < 90 or BETA6 > 270: PSUM6 = FSUM6 / (BKOR6 - BKAN6) / dkor
            #if BETA7 >= 90 and BETA7 <= 270: PSUM7 = FSUM7 / BKOR7 / dkor
            #if BETA7 < 90 or BETA7 > 270: PSUM7 = FSUM7 / (BKOR7 - BKAN7) / dkor

        ws.cell(row=1, column=60).value = "PSUM, MPa"
        ws.cell(row=mm + 1, column=60).value = PSUM
        ws.cell(row=1, column=61).value = "PSUM1, MPa"
        ws.cell(row=mm + 1, column=61).value = PSUM1
        ws.cell(row=1, column=62).value = "PSUM2, MPa"
        ws.cell(row=mm + 1, column=62).value = PSUM2
        ws.cell(row=1, column=63).value = "PSUM3, MPa"
        ws.cell(row=mm + 1, column=63).value = PSUM3
        ws.cell(row=1, column=64).value = "PSUM4, MPa"
        ws.cell(row=mm + 1, column=64).value = PSUM4
        ws.cell(row=1, column=65).value = "PSUM5, MPa"
        ws.cell(row=mm + 1, column=65).value = PSUM5
        #ws.cell(row=1, column=66).value = "PSUM6, MPa"
        #ws.cell(row=mm + 1, column=66).value = PSUM6
        #ws.cell(row=1, column=67).value = "PSUM7, MPa"
        #ws.cell(row=mm + 1, column=67).value = PSUM7

    N = nm
    DMS = 0
    DMS1 = 0
    DMS2 = 0
    DMS3 = 0
    DMS4 = 0
    DMS5 = 0
    DMS6 = 0
    DMS7 = 0

    PDM = 0
    PDM1 = 0
    PDM2 = 0
    PDM3 = 0
    PDM4 = 0
    PDM5 = 0
    PDM6 = 0
    PDM7 = 0

    PK = (SSCH / bsch) ** 2
    PK1 = (SKOR * 2 / (BKOR1 - BKAN1)) ** 2
    PK2 = (SKOR * 2 / (BKOR2 - BKAN2)) ** 2
    PK3 = (SKOR * 2 / (BKOR3 - BKAN3)) ** 2
    PK4 = (SKOR * 2 / (BKOR4 - BKAN4)) ** 2
    PK5 = (SKOR * 2 / (BKOR5 - BKAN5)) ** 2
    #PK6 = (SKOR * 2 / (BKOR6 - BKAN6)) ** 2
    #PK7 = (SKOR * 2 / (BKOR7 - BKAN7)) ** 2
    if BKAN3 == 0: PK3 = PK3 / 4

    for JJJ in range(1, 7):

        tsr = TVX

        for I2 in range(1, 301):
            tsr = tsr + 1
            if (ms == 8): WM = 6.182646 * math.exp(919.948132 / (tsr + 95)) / 10 ** 5
            if (ms == 10): WM = 10.631472 * math.exp(853.510463 / (tsr + 95)) / 10 ** 5
            if (ms == 12): WM = 5.557801 * math.exp(1013.400364 / (tsr + 95)) / 10 ** 5
            if (ms == 16): WM = 12.98 * math.exp(912.7546 / (tsr + 95)) / 10 ** 5
            # print(WM, tsr)
            if (JJJ == 1):
                s = (6.03 * 10 ** (-10) * (
                            dsch / bsch * math.log10(bsch / d0)) ** 0.5 * bsch * dsch * N * WM / 0.001 / (
                                 PM * (tsr - TVX)) ** 0.5) ** 0.5
                if (s <= SSCHT): break
                # print("JJJ = 1", WM)

            if (JJJ == 2):
                s = (0.000000001134 * ((BKOR1 - BKAN1) / 2 * dkor * N * WM / 0.001) / (
                            (PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR1 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break

            if (JJJ == 3):
                s = (0.000000001134 * ((BKOR2 - BKAN2) / 2 * dkor * N * WM / 0.001) / (
                            (PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR2 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break

            if (JJJ == 4):
                s = (0.000000001134 * ((BKOR3 - BKAN3) / 2 * dkor * N * WM / 0.001) / (
                            (PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR3 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (BKAN3 == 0): s = s1
                if (s <= SKORT): break

            if (JJJ == 5):
                s = (0.000000001134 * ((BKOR4 - BKAN4) / 2 * dkor * N * WM / 0.001) / (
                            (PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR4 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break

            if (JJJ == 6):
                s = (0.000000001134 * ((BKOR5 - BKAN5) / 2 * dkor * N * WM / 0.001) / (
                            (PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR5 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break

            '''if (JJJ == 7):
                s = (0.000000001134 * ((BKOR6 - BKAN6) / 2 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR6 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break

            if (JJJ == 8):
                s = (0.000000001134 * ((BKOR7 - BKAN7) / 2 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                s1 = (0.000000001134 * (BKOR7 * dkor * N * WM / 0.001) / ((PM * (tsr - TVX)) ** 0.5)) ** 0.5
                if (NW == 2): s = (s + s1) / 2
                if (s <= SKORT): break'''

        if (JJJ == 1):
            TT1 = tsr
            ws.cell(row=5, column=8).value = TT1
            if fl == 1:
                data.wm1[0] = WM
            elif fl == 2:
                data.wm2[0] = WM
            elif fl == 3:
                data.wm3[0] = WM
        if (JJJ == 2):
            TT2 = tsr
            ws.cell(row=6, column=8).value = TT2
            if fl == 1:
                data.wm1[1] = WM
            elif fl == 2:
                data.wm2[1] = WM
            elif fl == 3:
                data.wm3[1] = WM
        if (JJJ == 3):
            TT3 = tsr
            ws.cell(row=7, column=8).value = TT3
            if fl == 1:
                data.wm1[2] = WM
            elif fl == 2:
                data.wm2[2] = WM
            elif fl == 3:
                data.wm3[2] = WM
        if (JJJ == 4):
            TT4 = tsr
            ws.cell(row=8, column=8).value = TT4
            if fl == 1:
                data.wm1[3] = WM
            elif fl == 2:
                data.wm2[3] = WM
            elif fl == 3:
                data.wm3[3] = WM
        if (JJJ == 5):
            TT5 = tsr
            ws.cell(row=9, column=8).value = TT5
            if fl == 1:
                data.wm1[4] = WM
            elif fl == 2:
                data.wm2[4] = WM
            elif fl == 3:
                data.wm3[4] = WM
        if (JJJ == 6):
            TT6 = tsr
            ws.cell(row=10, column=8).value = TT6
            if fl == 1:
                data.wm1[5] = WM
            elif fl == 2:
                data.wm2[5] = WM
            elif fl == 3:
                data.wm3[5] = WM
        if (JJJ == 7):
            TT7 = tsr
            ws.cell(row=11, column=8).value = TT7
            if fl == 1:
                data.wm1[6] = WM
            elif fl == 2:
                data.wm2[6] = WM
            elif fl == 3:
                data.wm3[6] = WM
        if (JJJ == 8):
            TT8 = tsr
            ws.cell(row=12, column=8).value = TT8
            if fl == 1:
                data.wm1[7] = WM
            elif fl == 2:
                data.wm2[7] = WM
            elif fl == 3:
                data.wm3[7] = WM

        E = 0.8
        DELTAE = 0
        F = 0
        DELTAF = 0
        TETA = 1.57

        for JJ in range(1, 4):

            for j in range(1, 721):

                BETA = ws.cell(row=j + 1, column=13).value
                BETA1 = ws.cell(row=j + 1, column=18).value
                BETA2 = ws.cell(row=j + 1, column=23).value
                BETA3 = ws.cell(row=j + 1, column=28).value
                BETA4 = ws.cell(row=j + 1, column=33).value
                BETA5 = ws.cell(row=j + 1, column=38).value
                BETA6 = ws.cell(row=j + 1, column=43).value
                BETA7 = ws.cell(row=j + 1, column=48).value

                GAM = ws.cell(row=j + 1, column=14).value
                GAM1 = ws.cell(row=j + 1, column=19).value
                GAM2 = ws.cell(row=j + 1, column=24).value
                GAM3 = ws.cell(row=j + 1, column=29).value
                GAM4 = ws.cell(row=j + 1, column=34).value
                GAM5 = ws.cell(row=j + 1, column=39).value
                GAM6 = ws.cell(row=j + 1, column=44).value
                GAM7 = ws.cell(row=j + 1, column=49).value

                if (NW != 1):
                    if (BETA1 >= 90 and BETA1 <= 270): PK1 = (SKOR / BKOR1) ** 2
                    if (BETA1 < 90 or BETA1 > 270): PK1 = (SKOR * 2 / (BKOR1 - BKAN1)) ** 2
                    if (BETA2 >= 90 and BETA2 <= 270): PK2 = (SKOR / BKOR2) ** 2
                    if (BETA2 < 90 or BETA2 > 270): PK2 = (SKOR * 2 / (BKOR2 - BKAN2)) ** 2
                    if (BKAN3 != 0):
                        if (BETA3 >= 90 and BETA3 <= 270): PK3 = (SKOR / BKOR3) ** 2
                        if (BETA3 < 90 or BETA3 > 270): PK3 = (SKOR * 2 / (BKOR3 - BKAN3)) ** 2
                    if (BETA4 >= 90 and BETA4 <= 270): PK4 = (SKOR / BKOR4) ** 2
                    if (BETA4 < 90 or BETA4 > 270): PK4 = (SKOR * 2 / (BKOR4 - BKAN4)) ** 2
                    if (BETA5 >= 90 and BETA5 <= 270): PK5 = (SKOR / BKOR5) ** 2
                    if (BETA5 < 90 or BETA5 > 270): PK5 = (SKOR * 2 / (BKOR5 - BKAN5)) ** 2
                    '''if (BETA6 >= 90 and BETA6 <= 270): PK6 = (SKOR / BKOR6) ** 2
                    if (BETA6 < 90 or BETA6 > 270): PK6 = (SKOR * 2 / (BKOR6 - BKAN6)) ** 2
                    if (BETA7 >= 90 and BETA7 <= 270): PK7 = (SKOR / BKOR7) ** 2
                    if (BETA7 < 90 or BETA7 > 270): PK7 = (SKOR * 2 / (BKOR5 - BKAN7)) ** 2'''

                PSUM = ws.cell(row=j + 1, column=60).value
                PSUM1 = ws.cell(row=j + 1, column=61).value
                PSUM2 = ws.cell(row=j + 1, column=62).value
                PSUM3 = ws.cell(row=j + 1, column=63).value
                PSUM4 = ws.cell(row=j + 1, column=64).value
                PSUM5 = ws.cell(row=j + 1, column=65).value
                PSUM6 = ws.cell(row=j + 1, column=66).value
                PSUM7 = ws.cell(row=j + 1, column=67).value

                if (JJJ == 1): P = PK * PSUM * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 2): P = PK1 * PSUM1 * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 3): P = PK2 * PSUM2 * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 4): P = PK3 * PSUM3 * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 5): P = PK4 * PSUM4 * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 6): P = PK5 * PSUM5 * 10 ** 6 / 30 / ww / WM * Pi
                '''if (JJJ == 7): P = PK6 * PSUM6 * 10 ** 6 / 30 / ww / WM * Pi
                if (JJJ == 8): P = PK7 * PSUM7 * 10 ** 6 / 30 / ww / WM * Pi'''

                ws.cell(row=1, column=68).value = "P"
                ws.cell(row=1, column=69).value = "p1"
                ws.cell(row=1, column=70).value = "p2"
                ws.cell(row=1, column=71).value = "p3"
                ws.cell(row=1, column=72).value = "p4"
                ws.cell(row=1, column=73).value = "p5"
                ws.cell(row=1, column=74).value = "p6"
                ws.cell(row=1, column=75).value = "p7"

                if (JJJ == 1): ws.cell(row=j + 1, column=68).value = P
                if (JJJ == 2): ws.cell(row=j + 1, column=69).value = P
                if (JJJ == 3): ws.cell(row=j + 1, column=70).value = P
                if (JJJ == 4): ws.cell(row=j + 1, column=71).value = P
                if (JJJ == 5): ws.cell(row=j + 1, column=72).value = P
                if (JJJ == 6): ws.cell(row=j + 1, column=73).value = P
                if (JJJ == 7): ws.cell(row=j + 1, column=74).value = P
                if (JJJ == 8): ws.cell(row=j + 1, column=75).value = P

                B1 = j - 1
                if (B1 == 0): B1 = 720
                if (B1 == 1): B1 = 721
                B2 = j + 1
                if (B2 == 722): B2 = 2

                if (JJJ == 1): BET1 = ws.cell(row=B1, column=13).value
                if (JJJ == 2): BET1 = ws.cell(row=B1, column=18).value
                if (JJJ == 3): BET1 = ws.cell(row=B1, column=23).value
                if (JJJ == 4): BET1 = ws.cell(row=B1, column=28).value
                if (JJJ == 5): BET1 = ws.cell(row=B1, column=33).value
                if (JJJ == 6): BET1 = ws.cell(row=B1, column=38).value
                if (JJJ == 7): BET1 = ws.cell(row=B1, column=43).value
                if (JJJ == 8): BET1 = ws.cell(row=B1, column=48).value

                if (JJJ == 1): BET2 = ws.cell(row=B2, column=13).value
                if (JJJ == 2): BET2 = ws.cell(row=B2, column=18).value
                if (JJJ == 3): BET2 = ws.cell(row=B2, column=23).value
                if (JJJ == 4): BET2 = ws.cell(row=B2, column=28).value
                if (JJJ == 5): BET2 = ws.cell(row=B2, column=33).value
                if (JJJ == 6): BET2 = ws.cell(row=B2, column=38).value
                if (JJJ == 7): BET2 = ws.cell(row=B2, column=43).value
                if (JJJ == 8): BET2 = ws.cell(row=B2, column=48).value
                DELTAB = BET2 - BET1

                if (BET1 >= 270 and BET2 <= 90): DELTAB = DELTAB + 360
                if (BET1 <= 90 and BET2 >= 270): DELTAB = DELTAB - 360
                AJ = j

                Pi = 3.14159265
                E = E + DELTAE
                F = F + DELTAF
                if (abs(E) < 0.1): F = 0
                if (abs(E) < 0.1): E = 0.1
                if (abs(F) >= 90): TETA = 1.57
                if (abs(F) >= 90): E = E * (-1)
                if (F >= 90): F = F - 180
                if (F <= -90): F = F + 180
                if (F > 89 and F < 90): F = 89
                if (F > -90 and F < -89): F = -89
                D = math.tan(F * 0.017453)

                while True:

                    Pi = 3.14159265
                    T0 = 1 - E ** 2
                    T1 = E / (1 - (E * math.cos(TETA)) ** 2)
                    T2 = (T0) ** 0.5
                    T3 = math.atan(-T2 / E / math.sin(TETA))
                    if (T3 < 0):
                        T3 = T3 + Pi
                    T4 = 1 + 2 * E ** 2
                    T5 = math.sin(TETA) * T1 + T3 / T2
                    T6 = (T1 * (3 - T4 * (math.cos(TETA)) ** 2) + math.sin(TETA) * T4 * T3 / T2) / math.cos(TETA) / T0
                    T7 = T5 / T6
                    T8 = T6 / T0

                    if (T7 - D) < 0:
                        # 1
                        while T7 < D:
                            TETA = TETA - 0.01

                            Pi = 3.14159265
                            T0 = 1 - E ** 2
                            T1 = E / (1 - (E * math.cos(TETA)) ** 2)
                            T2 = (T0) ** 0.5
                            T3 = math.atan(-T2 / E / math.sin(TETA))
                            if (T3 < 0):
                                T3 = T3 + Pi
                            T4 = 1 + 2 * E ** 2
                            T5 = math.sin(TETA) * T1 + T3 / T2
                            T6 = (T1 * (3 - T4 * (math.cos(TETA)) ** 2) + math.sin(TETA) * T4 * T3 / T2) / math.cos(
                                TETA) / T0
                            T7 = T5 / T6
                            T8 = T6 / T0
                    elif (T7 - D) > 0:
                        while (T7 > D):
                            TETA = TETA + 0.01

                            Pi = 3.14159265
                            T0 = 1 - E ** 2
                            T1 = E / (1 - (E * math.cos(TETA)) ** 2)
                            T2 = (T0) ** 0.5
                            T3 = math.atan(-T2 / E / math.sin(TETA))
                            if (T3 < 0):
                                T3 = T3 + Pi
                            T4 = 1 + 2 * E ** 2
                            T5 = math.sin(TETA) * T1 + T3 / T2
                            T6 = (T1 * (3 - T4 * (math.cos(TETA)) ** 2) + math.sin(TETA) * T4 * T3 / T2) / math.cos(
                                TETA) / T0
                            T7 = T5 / T6
                            T8 = T6 / T0

                    ESF = 30 / Pi / E * P * abs(math.cos(F * 0.017453)) / T8
                    DELTAE = Pi / 360 * E * math.tan(TETA) * ESF

                    if (JJJ == 1): DELTAF = 0.5 * (1 + math.cos(AJ * 0.017453) / (
                                (1 / A00) ** 2 - math.sin(AJ * 0.017453) ** 2) ** 0.5 - DELTAB - ESF)
                    if (JJJ > 1): DELTAF = 0.5 * (1 - DELTAB - ESF)

                    if (E >= 0):
                        if ((ESF * math.sin(TETA + 1.57)) > (360 * DELTAE * math.cos(TETA + 1.57) / Pi / E)):
                            break
                        TETA = TETA + Pi
                    else:
                        if ((ESF * math.sin(TETA + 1.57)) < (360 * DELTAE * math.cos(TETA + 1.57) / Pi / E)):
                            break
                        TETA = TETA + Pi

                if (JJJ == 1): shmin = SSCH * (1 - abs(E)) / 2
                if (JJJ == 2): shmin1 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 3): shmin2 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 4): shmin3 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 5): shmin4 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 6): shmin5 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 7): shmin6 = SKOR * (1 - abs(E)) / 2
                if (JJJ == 8): shmin7 = SKOR * (1 - abs(E)) / 2

                if (JJJ == 1): SSR = 0
                if (JJJ == 2): SSR1 = 0
                if (JJJ == 3): SSR2 = 0
                if (JJJ == 4): SSR3 = 0
                if (JJJ == 5): SSR4 = 0
                if (JJJ == 6): SSR5 = 0
                if (JJJ == 7): SSR6 = 0
                if (JJJ == 8): SSR7 = 0

                if (JJJ == 1):
                    hmin = SSCH * (1 - abs(E)) / 2
                    if (shmin >= hmin):
                        shmin = hmin
                        ump = j
                    SSR = SSR + hmin
                    FHMIN = BETA + F
                    if (E < 0): FHMIN = FHMIN + 180
                    if (FHMIN < 0): FHMIN = FHMIN + 360
                    if (FHMIN > 360): FHMIN = FHMIN - 360
                    xg = (1 - (2 * hmin / SSCH)) * math.cos((90 - FHMIN) * rad)
                    yg = (1 - (2 * hmin / SSCH)) * math.sin((90 - FHMIN) * rad)

                    ugwt = FHMIN - BETA + GAM
                    if (ugwt > 360): ugwt = ugwt - 360
                    if (ugwt < 0): ugwt = ugwt + 360
                    if data.current_mode == 2: 
                        data.ugwt_mode1.append(ugwt)
                    elif data.current_mode == 3:
                        data.ugwt_mode2.append(ugwt)
                    elif data.current_mode == 4:
                        data.ugwt_mode3.append(ugwt)
                    xq = (1 - (2 * hmin / SSCH)) * math.cos((90 - ugwt) * rad)
                    yq = (1 - (2 * hmin / SSCH)) * math.sin((90 - ugwt) * rad)

                    ws.cell(row=1, column=76).value = "E"
                    ws.cell(row=j + 1, column=76).value = E
                    ws.cell(row=1, column=77).value = "xg"
                    ws.cell(row=j + 1, column=77).value = xg
                    ws.cell(row=1, column=78).value = "yg"
                    ws.cell(row=j + 1, column=78).value = yg
                    ws.cell(row=1, column=79).value = "xq"
                    ws.cell(row=j + 1, column=79).value = xq
                    ws.cell(row=1, column=80).value = "yq"
                    ws.cell(row=j + 1, column=80).value = yq
                    ws.cell(row=1, column=81).value = "hmin"
                    ws.cell(row=j + 1, column=81).value = hmin
                    ws.cell(row=1, column=82).value = "FHMIN"
                    ws.cell(row=j + 1, column=82).value = FHMIN

                if (JJJ == 2):
                    hmin1 = SKOR * (1 - abs(E)) / 2
                    if (shmin1 >= hmin1):
                        shmin1 = hmin1
                        ump1 = j
                    SSR1 = SSR1 + hmin1
                    FHMIN1 = BETA1 + F
                    if (E < 0): FHMIN1 = FHMIN1 + 180
                    if (FHMIN1 < 0): FHMIN1 = FHMIN1 + 360
                    if (FHMIN1 > 360): FHMIN1 = FHMIN1 - 360

                    xg1 = (1 - (2 * hmin1 / SKOR)) * math.cos((90 - FHMIN1) * rad)
                    yg1 = (1 - (2 * hmin1 / SKOR)) * math.sin((90 - FHMIN1) * rad)
                    ugwt1 = FHMIN1 - BETA1 + GAM1
                    if (ugwt1 > 360): ugwt1 = ugwt1 - 360
                    if (ugwt1 < 0): ugwt1 = ugwt1 + 360
                    if data.current_mode == 2: 
                        data.ugwt1_mode1.append(ugwt1)
                    elif data.current_mode == 3:
                        data.ugwt1_mode2.append(ugwt1)
                    elif data.current_mode == 4:
                        data.ugwt1_mode3.append(ugwt1)
                    xq1 = (1 - (2 * hmin1 / SKOR)) * math.cos((90 - ugwt1) * rad)
                    yq1 = (1 - (2 * hmin1 / SKOR)) * math.sin((90 - ugwt1) * rad)

                    ws.cell(row=1, column=84).value = "E1"
                    ws.cell(row=j + 1, column=84).value = E
                    ws.cell(row=1, column=85).value = "xg1"
                    ws.cell(row=j + 1, column=85).value = xg1
                    ws.cell(row=1, column=86).value = "yg1"
                    ws.cell(row=j + 1, column=86).value = yg1
                    ws.cell(row=1, column=87).value = "xq1"
                    ws.cell(row=j + 1, column=87).value = xq1
                    ws.cell(row=1, column=88).value = "yq1"
                    ws.cell(row=j + 1, column=88).value = yq1
                    ws.cell(row=1, column=89).value = "hmin1"
                    ws.cell(row=j + 1, column=89).value = hmin1
                    ws.cell(row=1, column=90).value = "FHMIN1"
                    ws.cell(row=j + 1, column=90).value = FHMIN1

                if (JJJ == 3):
                    hmin2 = SKOR * (1 - abs(E)) / 2
                    if (shmin2 >= hmin2):
                        shmin2 = hmin2
                        ump2 = j
                    SSR2 = SSR2 + hmin2
                    FHMIN2 = BETA2 + F
                    if (E < 0): FHMIN2 = FHMIN2 + 180
                    if (FHMIN2 < 0): FHMIN2 = FHMIN2 + 360
                    if (FHMIN2 > 360): FHMIN2 = FHMIN2 - 360
                    xg2 = (1 - (2 * hmin2 / SKOR)) * math.cos((90 - FHMIN2) * rad)
                    yg2 = (1 - (2 * hmin2 / SKOR)) * math.sin((90 - FHMIN2) * rad)
                    ugwt2 = FHMIN2 - BETA2 + GAM2
                    if (ugwt2 > 360): ugwt2 = ugwt2 - 360
                    if (ugwt2 < 0): ugwt2 = ugwt2 + 360
                    if data.current_mode == 2: 
                        data.ugwt2_mode1.append(ugwt2)
                    elif data.current_mode == 3:
                        data.ugwt2_mode2.append(ugwt2)
                    elif data.current_mode == 4:
                        data.ugwt2_mode3.append(ugwt2)
                    xq2 = (1 - (2 * hmin2 / SKOR)) * math.cos((90 - ugwt2) * rad)
                    yq2 = (1 - (2 * hmin2 / SKOR)) * math.sin((90 - ugwt2) * rad)

                    ws.cell(row=1, column=91).value = "E2"
                    ws.cell(row=j + 1, column=91).value = E
                    ws.cell(row=1, column=92).value = "xg2"
                    ws.cell(row=j + 1, column=92).value = xg2
                    ws.cell(row=1, column=93).value = "yg2"
                    ws.cell(row=j + 1, column=93).value = yg2
                    ws.cell(row=1, column=94).value = "xq2"
                    ws.cell(row=j + 1, column=94).value = xq2
                    ws.cell(row=1, column=95).value = "yq2"
                    ws.cell(row=j + 1, column=95).value = yq2
                    ws.cell(row=1, column=96).value = "hmin2"
                    ws.cell(row=j + 1, column=96).value = hmin2
                    ws.cell(row=1, column=97).value = "FHMIN2"
                    ws.cell(row=j + 1, column=97).value = FHMIN2

                if (JJJ == 4):
                    hmin3 = SKOR * (1 - abs(E)) / 2
                    if (shmin3 >= hmin3):
                        shmin3 = hmin3
                        ump3 = j
                    SSR3 = SSR3 + hmin3
                    FHMIN3 = BETA3 + F
                    if (E < 0): FHMIN3 = FHMIN3 + 180
                    if (FHMIN3 < 0): FHMIN3 = FHMIN3 + 360
                    if (FHMIN3 > 360): FHMIN3 = FHMIN3 - 360
                    xg3 = (1 - (2 * hmin3 / SKOR)) * math.cos((90 - FHMIN3) * rad)
                    yg3 = (1 - (2 * hmin3 / SKOR)) * math.sin((90 - FHMIN3) * rad)
                    ugwt3 = FHMIN3 - BETA3 + GAM3
                    if (ugwt3 > 360): ugwt3 = ugwt3 - 360
                    if (ugwt3 < 0): ugwt3 = ugwt3 + 360
                    if data.current_mode == 2: 
                        data.ugwt3_mode1.append(ugwt3)
                    elif data.current_mode == 3:
                        data.ugwt3_mode2.append(ugwt3)
                    elif data.current_mode == 4:
                        data.ugwt3_mode3.append(ugwt3)
                    xq3 = (1 - (2 * hmin3 / SKOR)) * math.cos((90 - ugwt3) * rad)
                    yq3 = (1 - (2 * hmin3 / SKOR)) * math.sin((90 - ugwt3) * rad)

                    ws.cell(row=1, column=98).value = "E3"
                    ws.cell(row=j + 1, column=98).value = E
                    ws.cell(row=1, column=99).value = "xg3"
                    ws.cell(row=j + 1, column=99).value = xg3
                    ws.cell(row=1, column=100).value = "yg3"
                    ws.cell(row=j + 1, column=100).value = yg3
                    ws.cell(row=1, column=101).value = "xq3"
                    ws.cell(row=j + 1, column=101).value = xq3
                    ws.cell(row=1, column=102).value = "yq3"
                    ws.cell(row=j + 1, column=102).value = yq3
                    ws.cell(row=1, column=103).value = "hmin3"
                    ws.cell(row=j + 1, column=103).value = hmin3
                    ws.cell(row=1, column=104).value = "FHMIN3"
                    ws.cell(row=j + 1, column=104).value = FHMIN3


                if (JJJ == 5):
                    hmin4 = SKOR * (1 - abs(E)) / 2
                    if (shmin4 >= hmin4):
                        shmin4 = hmin4
                        ump4 = j
                    SSR4 = SSR4 + hmin4
                    FHMIN4 = BETA4 + F
                    if (E < 0): FHMIN4 = FHMIN4 + 180
                    if (FHMIN4 < 0): FHMIN4 = FHMIN4 + 360
                    if (FHMIN4 > 360): FHMIN4 = FHMIN4 - 360
                    xg4 = (1 - (2 * hmin4 / SKOR)) * math.cos((90 - FHMIN4) * rad)
                    yg4 = (1 - (2 * hmin4 / SKOR)) * math.sin((90 - FHMIN4) * rad)
                    ugwt4 = FHMIN4 - BETA4 + GAM4
                    if (ugwt4 > 360): ugwt4 = ugwt4 - 360
                    if (ugwt4 < 0): ugwt4 = ugwt4 + 360
                    if data.current_mode == 2: 
                        data.ugwt4_mode1.append(ugwt4)
                    elif data.current_mode == 3:
                        data.ugwt4_mode2.append(ugwt4)
                    elif data.current_mode == 4:
                        data.ugwt4_mode3.append(ugwt4)
                    xq4 = (1 - (2 * hmin4 / SKOR)) * math.cos((90 - ugwt4) * rad)
                    yq4 = (1 - (2 * hmin4 / SKOR)) * math.sin((90 - ugwt4) * rad)

                    ws.cell(row=1, column=105).value = "E4"
                    ws.cell(row=j + 1, column=105).value = E
                    ws.cell(row=1, column=106).value = "xg4"
                    ws.cell(row=j + 1, column=106).value = xg4
                    ws.cell(row=1, column=107).value = "yg4"
                    ws.cell(row=j + 1, column=107).value = yg4
                    ws.cell(row=1, column=108).value = "xq4"
                    ws.cell(row=j + 1, column=108).value = xq4
                    ws.cell(row=1, column=109).value = "yq4"
                    ws.cell(row=j + 1, column=109).value = yq4
                    ws.cell(row=1, column=110).value = "hmin4"
                    ws.cell(row=j + 1, column=110).value = hmin4
                    ws.cell(row=1, column=111).value = "FHMIN4"
                    ws.cell(row=j + 1, column=111).value = FHMIN4

                if (JJJ == 6):
                    hmin5 = SKOR * (1 - abs(E)) / 2
                    if (shmin5 >= hmin5):
                        shmin5 = hmin5
                        ump5 = j
                    SSR5 = SSR5 + hmin5
                    FHMIN5 = BETA5 + F
                    if (E < 0): FHMIN5 = FHMIN5 + 180
                    if (FHMIN5 < 0): FHMIN5 = FHMIN5 + 360
                    if (FHMIN5 > 360): FHMIN5 = FHMIN5 - 360
                    xg5 = (1 - (2 * hmin5 / SKOR)) * math.cos((90 - FHMIN5) * rad)
                    yg5 = (1 - (2 * hmin5 / SKOR)) * math.sin((90 - FHMIN5) * rad)
                    ugwt5 = FHMIN5 - BETA5 + GAM5
                    if (ugwt5 > 360): ugwt5 = ugwt5 - 360
                    if (ugwt5 < 0): ugwt5 = ugwt5 + 360
                    xq5 = (1 - (2 * hmin5 / SKOR)) * math.cos((90 - ugwt5) * rad)
                    yq5 = (1 - (2 * hmin5 / SKOR)) * math.sin((90 - ugwt5) * rad)
                    if data.current_mode == 2: 
                        data.ugwt5_mode1.append(ugwt5)
                    elif data.current_mode == 3:
                        data.ugwt5_mode2.append(ugwt5)
                    elif data.current_mode == 4:
                        data.ugwt5_mode3.append(ugwt5)
                    ws.cell(row=1, column=112).value = "E5"
                    ws.cell(row=j + 1, column=112).value = E
                    ws.cell(row=1, column=113).value = "xg5"
                    ws.cell(row=j + 1, column=113).value = xg5
                    ws.cell(row=1, column=114).value = "yg5"
                    ws.cell(row=j + 1, column=114).value = yg5
                    ws.cell(row=1, column=115).value = "xq5"
                    ws.cell(row=j + 1, column=115).value = xq5
                    ws.cell(row=1, column=116).value = "yq5"
                    ws.cell(row=j + 1, column=116).value = yq5
                    ws.cell(row=1, column=117).value = "hmin5"
                    ws.cell(row=j + 1, column=117).value = hmin5
                    ws.cell(row=1, column=118).value = "FHMIN5"
                    ws.cell(row=j + 1, column=118).value = FHMIN5

                if (JJJ == 7):
                    hmin6 = SKOR * (1 - abs(E)) / 2
                    if (shmin6 >= hmin6):
                        shmin6 = hmin6
                        ump6 = j
                    SSR6 = SSR6 + hmin6
                    FHMIN6 = BETA6 + F
                    if (E < 0): FHMIN6 = FHMIN6 + 180
                    if (FHMIN6 < 0): FHMIN6 = FHMIN6 + 360
                    if (FHMIN6 > 360): FHMIN6 = FHMIN6 - 360
                    xg6 = (1 - (2 * hmin6 / SKOR)) * math.cos((90 - FHMIN6) * rad)
                    yg6 = (1 - (2 * hmin6 / SKOR)) * math.sin((90 - FHMIN6) * rad)
                    ugwt6 = FHMIN6 - BETA6 + GAM6
                    if (ugwt6 > 360): ugwt6 = ugwt6 - 360
                    if (ugwt6 < 0): ugwt6 = ugwt6 + 360
                    if data.current_mode == 2: 
                        data.ugwt6_mode1.append(ugwt6)
                    elif data.current_mode == 3:
                        data.ugwt6_mode2.append(ugwt6)
                    elif data.current_mode == 4:
                        data.ugwt6_mode3.append(ugwt6)
                    xq6 = (1 - (2 * hmin6 / SKOR)) * math.cos((90 - ugwt6) * rad)
                    yq6 = (1 - (2 * hmin6 / SKOR)) * math.sin((90 - ugwt6) * rad)

                    ws.cell(row=1, column=119).value = "E6"
                    ws.cell(row=j + 1, column=119).value = E
                    ws.cell(row=1, column=120).value = "xg6"
                    ws.cell(row=j + 1, column=120).value = xg6
                    ws.cell(row=1, column=121).value = "yg6"
                    ws.cell(row=j + 1, column=121).value = yg6
                    ws.cell(row=1, column=122).value = "xq6"
                    ws.cell(row=j + 1, column=122).value = xq6
                    ws.cell(row=1, column=123).value = "yq6"
                    ws.cell(row=j + 1, column=123).value = yq6
                    ws.cell(row=1, column=124).value = "hmin6"
                    ws.cell(row=j + 1, column=124).value = hmin6
                    ws.cell(row=1, column=125).value = "FHMIN6"
                    ws.cell(row=j + 1, column=125).value = FHMIN6

                if (JJJ == 8):
                    hmin7 = SKOR * (1 - abs(E)) / 2
                    if (shmin7 >= hmin7):
                        shmin7 = hmin7
                        ump7 = j
                    SSR7 = SSR7 + hmin7
                    FHMIN7 = BETA7 + F
                    if (E < 0): FHMIN7 = FHMIN7 + 180
                    if (FHMIN7 < 0): FHMIN7 = FHMIN7 + 360
                    if (FHMIN7 > 360): FHMIN7 = FHMIN7 - 360
                    xg7 = (1 - (2 * hmin7 / SKOR)) * math.cos((90 - FHMIN7) * rad)
                    yg7 = (1 - (2 * hmin7 / SKOR)) * math.sin((90 - FHMIN7) * rad)
                    ugwt7 = FHMIN7 - BETA7 + GAM7
                    if (ugwt7 > 360): ugwt7 = ugwt7 - 360
                    if (ugwt7 < 0): ugwt7 = ugwt7 + 360
                    if data.current_mode == 2: 
                        data.ugwt7_mode1.append(ugwt7)
                    elif data.current_mode == 3:
                        data.ugwt7_mode2.append(ugwt7)
                    elif data.current_mode == 4:
                        data.ugwt7_mode3.append(ugwt7)
                    xq7 = (1 - (2 * hmin7 / SKOR)) * math.cos((90 - ugwt7) * rad)
                    yq7 = (1 - (2 * hmin7 / SKOR)) * math.sin((90 - ugwt7) * rad)

                    ws.cell(row=1, column=126).value = "E7"
                    ws.cell(row=j + 1, column=126).value = E
                    ws.cell(row=1, column=127).value = "xg7"
                    ws.cell(row=j + 1, column=127).value = xg7
                    ws.cell(row=1, column=128).value = "yg7"
                    ws.cell(row=j + 1, column=128).value = yg7
                    ws.cell(row=1, column=129).value = "xq7"
                    ws.cell(row=j + 1, column=129).value = xq7
                    ws.cell(row=1, column=130).value = "yq7"
                    ws.cell(row=j + 1, column=130).value = yq7
                    ws.cell(row=1, column=131).value = "hmin7"
                    ws.cell(row=j + 1, column=131).value = hmin7
                    ws.cell(row=1, column=132).value = "FHMIN7"
                    ws.cell(row=j + 1, column=132).value = FHMIN7

                if (JJ == 3):
                    if (JJJ == 1): SC = PK
                    if (JJJ == 2): SC = PK1
                    if (JJJ == 3): SC = PK2
                    if (JJJ == 4): SC = PK3
                    if (JJJ == 5): SC = PK4
                    if (JJJ == 6): SC = PK5
                    #if (JJJ == 7): SC = PK6
                    #if (JJJ == 8): SC = PK7

                    Pi = 3.14159265
                    rad = 0.01745329
                    PMM = 0
                    for I2 in range(1, 181):
                        R1 = Pi * E * math.sin(I2 * rad + TETA) * ESF / 30
                        R2 = 12 * math.cos(I2 * rad + TETA) * DELTAE
                        R3 = (1 + E * math.cos(I2 * rad + TETA)) ** 3
                        PMN = (R1 - R2) * 3 * WM * N / R3 / SC / 10 ** 6
                        if (PMM <= PMN):
                            PMM = PMN
                            UG = I2

                    if (JJJ == 1):
                        dm = PMM
                        ugm = FHMIN + TETA * GRAD + UG - 180
                        # if (ugm > 720): ugm = ugm - 720
                        # if (ugm > 360): ugm = ugm - 360
                        # if (ugm < 0): ugm = ugm + 360
                        while ugm > 360:
                            ugm -= 360
                        while ugm < 0:
                            ugm += 360
                        ugw = ugm - BETA + GAM
                        while ugw > 360:
                            ugw -= 360
                        while ugw < 0:
                            ugw += 360
                        # if (ugw > 360): ugw = ugw - 360
                        # if (ugw < 0): ugw = ugw + 360
                        if (PDM <= dm):
                            PDM = dm
                            aug1 = j
                        DMS = DMS + dm

                        ws.cell(row=1, column=133).value = "dm"
                        ws.cell(row=j + 1, column=133).value = dm
                        ws.cell(row=1, column=134).value = "ugm"
                        ws.cell(row=j + 1, column=134).value = ugm
                        ws.cell(row=1, column=135).value = "ugw"
                        ws.cell(row=j + 1, column=135).value = ugw

                    if (JJJ == 2):
                        dm1 = PMM
                        ugm1 = FHMIN1 + TETA * GRAD + UG - 180
                        # if (ugm1 > 720): ugm1 = ugm1 - 720
                        # if (ugm1 > 360): ugm1 = ugm1 - 360
                        # if (ugm1 < 0): ugm1 = ugm1 + 
                        while ugm1 > 360:
                            ugm1 -= 360
                        while ugm1 < 0:
                            ugm1 += 360
                        ugw1 = ugm1 - BETA1 + GAM1
                        while ugw1 > 360:
                            ugw1 -= 360
                        while ugw1 < 0:
                            ugw1 += 360
                        # if (ugw1 > 360): ugw1 = ugw1 - 360
                        # if (ugw1 < 0): ugw1 = ugw1 + 360
                        if (PDM1 <= dm1):
                            PDM1 = dm1
                            aug2 = j
                        DMS1 = DMS1 + dm1

                        ws.cell(row=1, column=136).value = "dm1"
                        ws.cell(row=j + 1, column=136).value = dm1
                        ws.cell(row=1, column=137).value = "ugm1"
                        ws.cell(row=j + 1, column=137).value = ugm1
                        ws.cell(row=1, column=138).value = "ugw1"
                        ws.cell(row=j + 1, column=138).value = ugw1

                    if (JJJ == 3):
                        dm2 = PMM
                        ugm2 = FHMIN2 + TETA * GRAD + UG - 180
                        # if (ugm2 > 720): ugm2 = ugm2 - 720
                        # if (ugm2 > 360): ugm2 = ugm2 - 360
                        # if (ugm2 < 0): ugm2 = ugm2 + 360
                        while ugm2 > 360:
                            ugm2 -= 360
                        while ugm2 < 0:
                            ugm2 += 360
                        ugw2 = ugm2 - BETA2 + GAM2
                        # if (ugw2 > 360): ugw2 = ugw2 - 360
                        # if (ugw2 < 0): ugw2 = ugw2 + 360
                        while ugw2 > 360:
                            ugw2 -= 360
                        while ugw2 < 0:
                            ugw2 += 360
                        if (PDM2 <= dm2):
                            PDM2 = dm2
                            aug3 = j
                        DMS2 = DMS2 + dm2

                        ws.cell(row=1, column=139).value = "dm2"
                        ws.cell(row=j + 1, column=139).value = dm2
                        ws.cell(row=1, column=140).value = "ugm2"
                        ws.cell(row=j + 1, column=140).value = ugm2
                        ws.cell(row=1, column=141).value = "ugw2"
                        ws.cell(row=j + 1, column=141).value = ugw2

                    if (JJJ == 4):
                        dm3 = PMM
                        ugm3 = FHMIN3 + TETA * GRAD + UG - 180
                        # if (ugm3 > 720): ugm3 = ugm3 - 720
                        # if (ugm3 > 360): ugm3 = ugm3 - 360
                        # if (ugm3 < 0): ugm3 = ugm3 + 360
                        while ugm3 > 360:
                            ugm3 -= 360
                        while ugm3 < 0:
                            ugm3 +=360
                        ugw3 = ugm3 - BETA3 + GAM3
                        while ugw3 > 360:
                            ugw3 -= 360
                        while ugw3 < 0:
                            ugw3 +=360
                        # if (ugw3 > 360): ugw3 = ugw3 - 360
                        # if (ugw3 < 0): ugw3 = ugw3 + 360
                        if (PDM3 <= dm3):
                            PDM3 = dm3
                            aug4 = j
                        DMS3 = DMS3 + dm3

                        ws.cell(row=1, column=142).value = "dm3"
                        ws.cell(row=j + 1, column=142).value = dm3
                        ws.cell(row=1, column=143).value = "ugm3"
                        ws.cell(row=j + 1, column=143).value = ugm3
                        ws.cell(row=1, column=144).value = "ugw3"
                        ws.cell(row=j + 1, column=144).value = ugw3

                    if (JJJ == 5):
                        dm4 = PMM
                        ugm4 = FHMIN4 + TETA * GRAD + UG - 180
                        # if (ugm4 > 720): ugm4 = ugm4 - 720
                        # if (ugm4 > 360): ugm4 = ugm4 - 360
                        # if (ugm4 < 0): ugm4 = ugm4 + 360
                        while ugm4 > 360:
                            ugm4 -= 360
                        while ugm4 < 0:
                            ugm4 += 360
                        ugw4 = ugm4 - BETA4 + GAM4
                        while ugw4 > 360:
                            ugw4 -= 360
                        while ugw4 < 0:
                            ugw4 += 360
                        # if (ugw4 > 360): ugw4 = ugw4 - 360
                        # if (ugw4 < 0): ugw4 = ugw4 + 360
                        if (PDM4 <= dm4):
                            PDM4 = dm4
                            aug5 = j
                        DMS4 = DMS4 + dm4

                        ws.cell(row=1, column=145).value = "dm4"
                        ws.cell(row=j + 1, column=145).value = dm4
                        ws.cell(row=1, column=146).value = "ugm4"
                        ws.cell(row=j + 1, column=146).value = ugm4
                        ws.cell(row=1, column=147).value = "ugw4"
                        ws.cell(row=j + 1, column=147).value = ugw4

                    if (JJJ == 6):
                        dm5 = PMM
                        ugm5 = FHMIN5 + TETA * GRAD + UG - 180
                        # if (ugm5 > 720): ugm5 = ugm5 - 720
                        # if (ugm5 > 360): ugm5 = ugm5 - 360
                        # if (ugm5 < 0): ugm5 = ugm5 + 360
                        while ugm5 > 360:
                            ugm5 -= 360
                        while ugm5 < 0:
                            ugm5 +=360
                        ugw5 = ugm5 - BETA5 + GAM5
                        while ugw5 > 360:
                            ugw5 -= 360
                        while ugw5 < 0:
                            ugw5 +=360
                        # if (ugw5 > 360): ugw5 = ugw5 - 360
                        # if (ugw5 < 0): ugw5 = ugw5 + 360
                        if (PDM5 <= dm5):
                            PDM5 = dm5
                            aug6 = j
                        DMS5 = DMS5 + dm5

                        ws.cell(row=1, column=148).value = "dm5"
                        ws.cell(row=j + 1, column=148).value = dm5
                        ws.cell(row=1, column=149).value = "ugm5"
                        ws.cell(row=j + 1, column=149).value = ugm5
                        ws.cell(row=1, column=150).value = "ugw5"
                        ws.cell(row=j + 1, column=150).value = ugw5

                    if (JJJ == 7):
                        dm6 = PMM
                        ugm6 = FHMIN6 + TETA * GRAD + UG - 180
                        # if (ugm6 > 720): ugm6 = ugm6 - 720
                        # if (ugm6 > 360): ugm6 = ugm6 - 360
                        # if (ugm6 < 0): ugm6 = ugm6 + 360
                        while ugm6 > 360:
                            ugm6 -= 360
                        while ugm6 < 0:
                            ugm6 += 360
                        ugw6 = ugm6 - BETA6 + GAM6
                        while ugw6 > 360:
                            ugw6 -= 360
                        while ugw6 < 0:
                            ugw6 += 360
                        # if (ugw6 > 360): ugw6 = ugw6 - 360
                        # if (ugw6 < 0): ugw6 = ugw6 + 360
                        if (PDM6 <= dm6):
                            PDM6 = dm6
                            aug6 = j
                        DMS6 = DMS6 + dm6

                        ws.cell(row=1, column=151).value = "dm6"
                        ws.cell(row=j + 1, column=151).value = dm6
                        ws.cell(row=1, column=152).value = "ugm6"
                        ws.cell(row=j + 1, column=152).value = ugm6
                        ws.cell(row=1, column=153).value = "ugw6"
                        ws.cell(row=j + 1, column=153).value = ugw6

                    if (JJJ == 8):
                        dm7 = PMM
                        ugm7 = FHMIN7 + TETA * GRAD + UG - 180
                        # if (ugm7 > 720): ugm7 = ugm7 - 720
                        # if (ugm7 > 360): ugm7 = ugm7 - 360
                        # if (ugm7 < 0): ugm7 = ugm7 + 360
                        while ugm7 > 360:
                            ugm7 -= 360
                        while ugm7 < 0:
                            ugm7 +=360
                        ugw7 = ugm7 - BETA7 + GAM7
                        while ugw7 > 360:
                            ugw7 -= 360
                        while ugw7 < 0:
                            ugw7 +=360
                        # if (ugw7 > 360): ugw7 = ugw7 - 360
                        # if (ugw7 < 0): ugw7 = ugw7 + 360
                        if (PDM7 <= dm7):
                            PDM7 = dm7
                            aug7 = j
                        DMS7 = DMS7 + dm7

                        ws.cell(row=1, column=154).value = "dm7"
                        ws.cell(row=j + 1, column=154).value = dm7
                        ws.cell(row=1, column=155).value = "ugm7"
                        ws.cell(row=j + 1, column=155).value = ugm7
                        ws.cell(row=1, column=156).value = "ugw7"
                        ws.cell(row=j + 1, column=156).value = ugw7

    DMS = DMS / 720
    DMS1 = DMS1 / 720
    DMS2 = DMS2 / 720
    DMS3 = DMS3 / 720
    DMS4 = DMS4 / 720
    DMS5 = DMS5 / 720
    DMS6 = DMS6 / 720
    DMS7 = DMS7 / 720

    ws.cell(row=2, column=158).value = "DMS"
    ws.cell(row=2, column=159).value = DMS
    ws.cell(row=3, column=158).value = "DMS1"
    ws.cell(row=3, column=159).value = DMS1
    ws.cell(row=4, column=158).value = "DMS2"
    ws.cell(row=4, column=159).value = DMS2
    ws.cell(row=5, column=158).value = "DMS3"
    ws.cell(row=5, column=159).value = DMS3
    ws.cell(row=6, column=158).value = "DMS4"
    ws.cell(row=6, column=159).value = DMS4
    ws.cell(row=7, column=158).value = "DMS5"
    ws.cell(row=7, column=159).value = DMS5
    ws.cell(row=8, column=158).value = "DMS6"
    ws.cell(row=8, column=159).value = DMS6
    ws.cell(row=9, column=158).value = "DMS7"
    ws.cell(row=9, column=159).value = DMS7

    SSR = SSR / 720
    SSR1 = SSR1 / 720
    SSR2 = SSR2 / 720
    SSR3 = SSR3 / 720
    SSR4 = SSR4 / 720
    '''SSR5 = SSR5 / 720
    SSR6 = SSR6 / 720
    SSR7 = SSR7 / 720'''

    ws.cell(row=12, column=158).value = "SSR"
    ws.cell(row=12, column=159).value = SSR
    ws.cell(row=13, column=158).value = "SSR1"
    ws.cell(row=13, column=159).value = SSR1
    ws.cell(row=14, column=158).value = "SSR2"
    ws.cell(row=14, column=159).value = SSR2
    ws.cell(row=15, column=158).value = "SSR3"
    ws.cell(row=15, column=159).value = SSR3
    ws.cell(row=16, column=158).value = "SSR4"
    ws.cell(row=16, column=159).value = SSR4
    '''ws.cell(row=17, column=158).value = "SSR5"
    ws.cell(row=17, column=159).value = SSR5
    ws.cell(row=18, column=158).value = "SSR6"
    ws.cell(row=18, column=159).value = SSR6
    ws.cell(row=19, column=158).value = "SSR7"
    ws.cell(row=19, column=159).value = SSR7'''

    '''
    For Jp = 1 To 720
        hmin = ws.cell(row=Jp, column=161).value
        hmin1 = ws.cell(row=Jp, column=162).value
        hmin2 = ws.cell(row=Jp, column=163).value
        hmin3 = ws.cell(row=Jp, column=164).value
        hmin4 = ws.cell(row=Jp, column=165).value
        hmin5 = ws.cell(row=Jp, column=166).value
        hmin6 = ws.cell(row=Jp, column=167).value
        hmin7 = ws.cell(row=Jp, column=168).value

        hmin = hmin * 1000
        hmin1 = hmin1 * 1000
        hmin2 = hmin2 * 1000
        hmin3 = hmin3 * 1000
        hmin4 = hmin4 * 1000
        hmin5 = hmin5 * 1000
        hmin6 = hmin5 * 1000
        hmin7 = hmin5 * 1000
    '''
    ws5 = wb['5']

    ws5.cell(1, 4).value = nm
    ws5.cell(2, 4).value = dsch
    ws5.cell(3, 4).value = dkor
    
    ws5.cell(4, 4).value = bsch
    ws5.cell(5, 4).value = BKOR1
    ws5.cell(6, 4).value = BKOR2
    ws5.cell(7, 4).value = BKOR3
    ws5.cell(8, 4).value = BKOR4
    ws5.cell(9, 4).value = BKOR5
    # ws5.cell(10, 4).value = BKOR6
    # ws5.cell(11, 4).value = BKOR7
    
    ws5.cell(12, 4).value = BKAN1
    ws5.cell(13, 4).value = BKAN2
    ws5.cell(14, 4).value = BKAN3
    ws5.cell(15, 4).value = BKAN4
    ws5.cell(16, 4).value = BKAN5
    # ws5.cell(17, 4).value = BKAN6
    # ws5.cell(18, 4).value = BKAN7
    
    ws5.cell(33, 4).value = TVX
    ws5.cell(34, 4).value = d0
    ws5.cell(40, 4).value = PM

    ws5.cell(41, 4).value = SSCH
    ws5.cell(42, 4).value = SKOR
    ws5.cell(43, 4).value = lw
    ws5.cell(44, 4).value = SSCHT
    ws5.cell(45, 4).value = SKORT
    ws5.cell(46, 4).value = NW
    ws5.cell(47, 4).value = ms
    ws5.cell(48, 4).value = fks
    ws5.cell(49, 4).value = fkk
    ws5.cell(50, 4).value = r
    ws5.cell(51, 4).value = l


    wb.save(filename + '/data_l4_res.xlsx')

def code_5(filename, expert):
    wb = load_workbook(filename + '/data_l4_res.xlsx')
    ws = wb["5"]
    ws4 = wb["4"]

    NM = ws.cell(1, 4).value
    DSCH = ws.cell(2, 4).value
    DKOR = ws.cell(3, 4).value
    
    BSCH = ws.cell(4, 4).value
    BKOR1 = ws.cell(5, 4).value
    BKOR2 = ws.cell(6, 4).value
    BKOR3 = ws.cell(7, 4).value
    BKOR4 = ws.cell(8, 4).value
    BKOR5 = ws.cell(9, 4).value
    # BKOR6 = ws.cell(10, 4).value
    # BKOR7 = ws.cell(11, 4).value
    
    BKAN1 = ws.cell(12, 4).value
    BKAN2 = ws.cell(13, 4).value
    BKAN3 = ws.cell(14, 4).value
    BKAN4 = ws.cell(15, 4).value
    BKAN5 = ws.cell(16, 4).value
    # BKAN6 = ws.cell(17, 4).value
    # BKAN7 = ws.cell(18, 4).value
    
    TVX = ws.cell(33, 4).value
    d0 = ws.cell(34, 4).value
    PM = ws.cell(40, 4).value
    SSCH = ws.cell(41, 4).value
    SKOR = ws.cell(42, 4).value
    lw = ws.cell(43, 4).value
    SSCHT = ws.cell(44, 4).value
    SKORT = ws.cell(45, 4).value
    NW = ws.cell(46, 4).value
    ms = ws.cell(47, 4).value
    FKS = ws.cell(48, 4).value
    FKK = ws.cell(49, 4).value
    r = ws.cell(50, 4).value
    l = ws.cell(51, 4).value

    TN = 0
    TN1 = 0
    TN2 = 0
    TN3 = 0
    TN4 = 0
    TN5 = 0

    kk = expert.root_bearing
    # print(kk)
    k = expert.cylinder

    Pi = 3.1415926535
    rad = 0.01745329252
    GRAD = 57.29577951
    
    ww = Pi * NM / 30
    A00 = r / l
      

      
    PF = (BSCH / DSCH) ** 2
    PF1 = ((BKOR1 - BKAN1) / DKOR) ** 2
    PF2 = ((BKOR2 - BKAN2) / DKOR) ** 2
    PF3 = ((BKOR3 - BKAN3) / DKOR) ** 2
    PF4 = ((BKOR4 - BKAN4) / DKOR) ** 2
    PF5 = ((BKOR5 - BKAN5) / DKOR) ** 2
    # PF6 = ((BKOR6 - BKAN6) / DKOR) ** 2
    # PF7 = ((BKOR7 - BKAN7) / DKOR) ** 2
    for JJJ in range(1, kk + 2):
        if JJJ == 1:
            ws.cell(1, 162).value = "TN"
            for J in range(1, 721):
                e = ws4.cell(J + 1, 76).value
                p = ws4.cell(J + 1, 68).value
                PSM = ws4.cell(J + 1, 12).value
                e = abs(e)
                SOD = p * 30 * PF / Pi
                FT = Pi / SOD / (1 - e ** 2) ** 0.5
                FNT = 7 * PSM * FT * NM * SSCH / 10 ** 8 * 0.7355
                TN = TN + FNT
                ws.cell(J + 1, 162).value = FNT
        elif JJJ == 2:
            ws.cell(1, 163).value = "TN1"
            for J in range(1, 721):
                e1 = ws4.cell(J + 1, 84).value
                p1 = ws4.cell(J + 1, 69).value
                PSM1 = ws4.cell(J + 1, 17).value
                e1 = abs(e1)
                SOD1 = p1 * 30 * PF1 / Pi
                FT1 = Pi / SOD1 / (1 - e1 ** 2) ** 0.5
                FNT1 = 7 * PSM1 * FT1 * NM * SSCH / 10 ** 8 * 0.7355
                TN1 = TN1 + FNT1
                ws.cell(J + 1, 163).value = FNT1
        elif JJJ == 3:
            ws.cell(1, 164).value = "TN2"
            for J in range(1, 721):
                e2 = ws4.cell(J + 1, 98).value
                p2 = ws4.cell(J + 1, 70).value
                PSM2 = ws4.cell(J + 1, 22).value
                e2 = abs(e2)
                SOD2 = p2 * 30 * PF2 / Pi
                FT2 = Pi / SOD2 / (1 - e2 ** 2) ** 0.5
                FNT2 = 7 * PSM2 * FT2 * NM * SSCH / 10 ** 8 * 0.7355
                TN2 = TN2 + FNT2
                ws.cell(J + 1, 164).value = FNT2
        elif JJJ == 4:
            ws.cell(1, 165).value = "TN3"
            for J in range(1, 721):
                e3 = ws4.cell(J + 1, 91).value
                p3 = ws4.cell(J + 1, 71).value
                PSM3 = ws4.cell(J + 1, 27).value
                e3 = abs(e3)
                SOD3 = p3 * 30 * PF3 / Pi
                FT3 = Pi / SOD3 / (1 - e3 ** 2) ** 0.5
                FNT3 = 7 * PSM3 * FT3 * NM * SSCH / 10 ** 8 * 0.7355
                TN3 = TN3 + FNT3
                ws.cell(J + 1, 165).value = FNT3
        elif JJJ == 5:
            ws.cell(1, 166).value = "TN4"
            for J in range(1, 721):
                e4 = ws4.cell(J + 1, 105).value
                p4 = ws4.cell(J + 1, 72).value
                PSM4 = ws4.cell(J + 1, 32).value
                e4 = abs(e4)
                SOD4 = p4 * 30 * PF4 / Pi
                FT4 = Pi / SOD4 / (1 - e4 ** 2) ** 0.5
                FNT4 = 7 * PSM4 * FT4 * NM * SSCH / 10 ** 8 * 0.7355
                TN4 = TN4 + FNT4
                ws.cell(J + 1, 166).value = FNT4
        elif JJJ == 6:
            ws.cell(1, 167).value = "TN5"
            for J in range(1, 721):
                e5 = ws4.cell(J + 1, 112).value
                p5 = ws4.cell(J + 1, 73).value
                PSM5 = ws4.cell(J + 1, 37).value
                e5 = abs(e5)
                SOD5 = p5 * 30 * PF5 / Pi
                FT5 = Pi / SOD5 / (1 - e5 ** 2) ** 0.5
                FNT5 = 7 * PSM5 * FT5 * NM * SSCH / 10 ** 8 * 0.7355
                TN5 = TN5 + FNT5
                ws.cell(J + 1, 167).value = FNT5
        # elif JJJ == 7:
        #     ws.cell(1, 168).value = "TN6"
        #     for J in range(1, 721):
        #         e6 = ws.cell(J + 1, 119).value
        #         p6 = ws.cell(J + 1, 74).value
        #         PSM6 = ws.cell(J + 1, 66).value
        #         e6 = abs(e6)
        #         SOD6 = p6 * 30 * PF6 / Pi
        #         FT6 = Pi / SOD6 / (1 - e6 ** 2) ** 0.5
        #         FNT6 = 7 * PSM6 * FT6 * NM * SSCH / 10 ** 8 * 0.7355
        #         TN6 = TN6 + FNT6
        #         ws.cell(J + 1, 168).value = FNT6
        # elif JJJ == 8:
        #     ws.cell(1, 169).value = "TN7"
        #     for J in range(1, 721):
        #         e7 = ws.cell(J + 1, 126).value
        #         p7 = ws.cell(J + 1, 75).value
        #         PSM7 = ws.cell(J + 1, 67).value
        #         e7 = abs(e7)
        #         SOD7 = p7 * 30 * PF7 / Pi
        #         FT7 = Pi / SOD7 / (1 - e7 ** 2) ** 0.5
        #         FNT7 = 7 * PSM7 * FT7 * NM * SSCH / 10 ** 8 * 0.7355
        #         TN7 = TN7 + FNT7
        #         ws.cell(J + 1, 169).value = FNT7

    EK = e
    if abs(e - EK) <= 0.01:
        TN /= 720
        TN1 /= 720
        TN2 /= 720
        TN3 /= 720
        TN4 /= 720
        TN5 /= 720

        PTN = k * TN + TN1 + TN2 + TN3 + TN4 + TN5
        ws.cell(22, 158).value = "Summ TN"
        ws.cell(22, 159).value = PTN
        ws.cell(23, 158).value = "TN"
        ws.cell(23, 159).value = TN
        ws.cell(24, 158).value = "TN1"
        ws.cell(24, 159).value = TN1
        ws.cell(25, 158).value = "TN2"
        ws.cell(25, 159).value = TN2
        ws.cell(26, 158).value = "TN3"
        ws.cell(26, 159).value = TN3
        ws.cell(27, 158).value = "TN4"
        ws.cell(27, 159).value = TN4
        ws.cell(28, 158).value = "TN5"
        ws.cell(28, 159).value = TN5
        # ws.cell(29, 158).value = "TN6"
        # ws.cell(29, 159).value = TN6
        # ws.cell(30, 158).value = "TN5"
        # ws.cell(30, 159).value = TN7

        for o in range(2, 363):
            ws.cell(o, 171).value = 0
            ws.cell(o, 172).value = 0
            ws.cell(o, 173).value = 0
            ws.cell(o, 174).value = 0
            ws.cell(o, 175).value = 0
            ws.cell(o, 176).value = 0
            ws.cell(o, 177).value = 0
            ws.cell(o, 178).value = 0
            ws.cell(o, 181).value = 0
            ws.cell(o, 182).value = 0
            ws.cell(o, 183).value = 0
            ws.cell(o, 184).value = 0
            ws.cell(o, 185).value = 0
            ws.cell(o, 186).value = 0
            ws.cell(o, 187).value = 0
            ws.cell(o, 188).value = 0

        HMD = 1.5 * FKS * DSCH / 100000
        HMK = 1.5 * FKK * DKOR / 100000

        for FFF in range(1, kk + 2):
            if FFF == 1:
                for J in range(1, 721):
                    hmin = ws4.cell(J + 1, 81).value
                    if hmin < HMD:
                        beta = round(ws4.cell(J + 1, 13).value)
                        FFTbet = ws.cell(beta + 2, 171).value
                        FFTbet = FFTbet + 1 / hmin
                        ws.cell(beta + 2, 171).value = FFTbet
                        Gam = round(ws4.cell(J + 1, 14).value)
                        FFTgam = ws.cell(Gam + 2, 181).value
                        FFTgam = FFTgam + 1 / hmin
                        ws.cell(Gam + 2, 181).value = FFTgam
            if FFF == 2:
                for J in range(1, 721):
                    hmin1 = ws4.cell(J + 1, 89).value
                    if hmin1 < HMK:
                        beta1 = round(ws4.cell(J + 1, 18).value)
                        FFTbet = ws.cell(beta1 + 2, 172).value
                        FFTbet = FFTbet + 1 / hmin1
                        ws.cell(beta1 + 2, 172).value = FFTbet
                        Gam1 = round(ws4.cell(J + 1, 19).value)
                        FFTgam = ws.cell(Gam1 + 2, 182).value
                        FFTgam = FFTgam + 1 / hmin1
                        ws.cell(Gam1 + 2, 182).value = FFTgam
            if FFF == 3:
                for J in range(1, 721):
                    hmin2 = ws4.cell(J + 1, 96).value
                    if hmin2 < HMK:
                        beta2 = round(ws4.cell(J + 1, 23).value)
                        FFTbet = ws.cell(beta2 + 2, 173).value
                        FFTbet = FFTbet + 1 / hmin2
                        ws.cell(beta2 + 2, 173).value = FFTbet
                        Gam2 = round(ws4.cell(J + 1, 24).value)
                        FFTgam = ws.cell(Gam2 + 2, 183).value
                        FFTgam = FFTgam + 1 / hmin2
                        ws.cell(Gam2 + 2, 183).value = FFTgam
            if FFF == 4:
                for J in range(1, 721):
                    hmin3 = ws4.cell(J + 1, 103).value
                    if hmin3 < HMK:
                        beta3 = round(ws4.cell(J + 1, 28).value)
                        FFTbet = ws.cell(beta3 + 2, 174).value
                        FFTbet = FFTbet + 1 / hmin3
                        ws.cell(beta3 + 2, 174).value = FFTbet
                        Gam3 = round(ws4.cell(J + 1, 29).value)
                        FFTgam = ws.cell(Gam3 + 2, 184).value
                        FFTgam = FFTgam + 1 / hmin3
                        ws.cell(Gam3 + 2, 184).value = FFTgam
            if FFF == 5:
                for J in range(1, 721):
                    hmin4 = ws4.cell(J + 1, 110).value
                    if hmin4 < HMK:
                        beta4 = round(ws4.cell(J + 1, 33).value)
                        FFTbet = ws.cell(beta4 + 2, 175).value
                        FFTbet = FFTbet + 1 / hmin4
                        ws.cell(beta4 + 2, 175).value = FFTbet
                        Gam4 = round(ws4.cell(J + 1, 34).value)
                        FFTgam = ws.cell(Gam4 + 2, 185).value
                        FFTgam = FFTgam + 1 / hmin4
                        ws.cell(Gam4 + 2, 185).value = FFTgam
            if FFF == 6:
                for J in range(1, 721):
                    hmin5 = ws4.cell(J + 1, 117).value
                    if hmin5 < HMK:
                        beta5 = round(ws4.cell(J + 1, 38).value)
                        FFTbet = ws.cell(beta5 + 2, 176).value
                        FFTbet = FFTbet + 1 / hmin5
                        ws.cell(beta5 + 2, 176).value = FFTbet
                        Gam5 = round(ws4.cell(J + 1, 39).value)
                        FFTgam = ws.cell(Gam5 + 2, 186).value
                        FFTgam = FFTgam + 1 / hmin5
                        ws.cell(Gam5 + 2, 186).value = FFTgam
            if FFF == 7:
                for J in range(1, 721):
                    hmin6 = ws4.cell(J + 1, 124).value
                    if hmin6 < HMK:
                        beta6 = round(ws4.cell(J + 1, 43).value)
                        FFTbet = ws.cell(beta6 + 2, 177).value
                        FFTbet = FFTbet + 1 / hmin6
                        ws.cell(beta6 + 2, 177).value = FFTbet
                        Gam6 = round(ws4.cell(J + 1, 44).value)
                        FFTgam = ws.cell(Gam6 + 2, 187).value
                        FFTgam = FFTgam + 1 / hmin6
                        ws.cell(Gam6 + 2, 187).value = FFTgam
            if FFF == 8:
                for J in range(1, 721):
                    hmin7 = ws4.cell(J + 1, 131).value
                    if hmin7 < HMK:
                        beta7 = round(ws4.cell(J + 1, 48).value)
                        FFTbet = ws.cell(beta7 + 1, 178).value
                        FFTbet = FFTbet + 1 / hmin7
                        ws.cell(beta7 + 1, 178).value = FFTbet
                        Gam7 = round(ws4.cell(J + 1, 49).value)
                        FFTgam = ws.cell(Gam7 + 2, 188).value
                        FFTgam = FFTgam + 1 / hmin7
                        ws.cell(Gam7 + 2, 188).value = FFTgam
    wb.save(filename + '/data_l4_res.xlsx')